wm831x-otp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/otp.h>
  22. /* In bytes */
  23. #define WM831X_UNIQUE_ID_LEN 16
  24. /* Read the unique ID from the chip into id */
  25. static int wm831x_unique_id_read(struct wm831x *wm831x, char *id)
  26. {
  27. int i, val;
  28. for (i = 0; i < WM831X_UNIQUE_ID_LEN / 2; i++) {
  29. val = wm831x_reg_read(wm831x, WM831X_UNIQUE_ID_1 + i);
  30. if (val < 0)
  31. return val;
  32. id[i * 2] = (val >> 8) & 0xff;
  33. id[(i * 2) + 1] = val & 0xff;
  34. }
  35. return 0;
  36. }
  37. static ssize_t wm831x_unique_id_show(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct wm831x *wm831x = dev_get_drvdata(dev);
  41. int i, rval;
  42. char id[WM831X_UNIQUE_ID_LEN];
  43. ssize_t ret = 0;
  44. rval = wm831x_unique_id_read(wm831x, id);
  45. if (rval < 0)
  46. return 0;
  47. for (i = 0; i < WM831X_UNIQUE_ID_LEN; i++)
  48. ret += sprintf(&buf[ret], "%02x", buf[i]);
  49. ret += sprintf(&buf[ret], "\n");
  50. return ret;
  51. }
  52. static DEVICE_ATTR(unique_id, 0444, wm831x_unique_id_show, NULL);
  53. int wm831x_otp_init(struct wm831x *wm831x)
  54. {
  55. int ret;
  56. ret = device_create_file(wm831x->dev, &dev_attr_unique_id);
  57. if (ret != 0)
  58. dev_err(wm831x->dev, "Unique ID attribute not created: %d\n",
  59. ret);
  60. return ret;
  61. }
  62. void wm831x_otp_exit(struct wm831x *wm831x)
  63. {
  64. device_remove_file(wm831x->dev, &dev_attr_unique_id);
  65. }