wm831x-otp.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * wm831x-otp.c -- OTP for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/bcd.h>
  18. #include <linux/delay.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/random.h>
  21. #include <linux/mfd/wm831x/core.h>
  22. #include <linux/mfd/wm831x/otp.h>
  23. /* In bytes */
  24. #define WM831X_UNIQUE_ID_LEN 16
  25. /* Read the unique ID from the chip into id */
  26. static int wm831x_unique_id_read(struct wm831x *wm831x, char *id)
  27. {
  28. int i, val;
  29. for (i = 0; i < WM831X_UNIQUE_ID_LEN / 2; i++) {
  30. val = wm831x_reg_read(wm831x, WM831X_UNIQUE_ID_1 + i);
  31. if (val < 0)
  32. return val;
  33. id[i * 2] = (val >> 8) & 0xff;
  34. id[(i * 2) + 1] = val & 0xff;
  35. }
  36. return 0;
  37. }
  38. static ssize_t wm831x_unique_id_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct wm831x *wm831x = dev_get_drvdata(dev);
  42. int i, rval;
  43. char id[WM831X_UNIQUE_ID_LEN];
  44. ssize_t ret = 0;
  45. rval = wm831x_unique_id_read(wm831x, id);
  46. if (rval < 0)
  47. return 0;
  48. for (i = 0; i < WM831X_UNIQUE_ID_LEN; i++)
  49. ret += sprintf(&buf[ret], "%02x", buf[i]);
  50. ret += sprintf(&buf[ret], "\n");
  51. return ret;
  52. }
  53. static DEVICE_ATTR(unique_id, 0444, wm831x_unique_id_show, NULL);
  54. int wm831x_otp_init(struct wm831x *wm831x)
  55. {
  56. char uuid[WM831X_UNIQUE_ID_LEN];
  57. int ret;
  58. ret = device_create_file(wm831x->dev, &dev_attr_unique_id);
  59. if (ret != 0)
  60. dev_err(wm831x->dev, "Unique ID attribute not created: %d\n",
  61. ret);
  62. ret = wm831x_unique_id_read(wm831x, uuid);
  63. if (ret == 0)
  64. add_device_randomness(uuid, sizeof(uuid));
  65. else
  66. dev_err(wm831x->dev, "Failed to read UUID: %d\n", ret);
  67. return ret;
  68. }
  69. void wm831x_otp_exit(struct wm831x *wm831x)
  70. {
  71. device_remove_file(wm831x->dev, &dev_attr_unique_id);
  72. }