ab3100-otp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * drivers/mfd/ab3100_otp.c
  3. *
  4. * Copyright (C) 2007-2009 ST-Ericsson AB
  5. * License terms: GNU General Public License (GPL) version 2
  6. * Driver to read out OTP from the AB3100 Mixed-signal circuit
  7. * Author: Linus Walleij <linus.walleij@stericsson.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/abx500.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/seq_file.h>
  17. /* The OTP registers */
  18. #define AB3100_OTP0 0xb0
  19. #define AB3100_OTP1 0xb1
  20. #define AB3100_OTP2 0xb2
  21. #define AB3100_OTP3 0xb3
  22. #define AB3100_OTP4 0xb4
  23. #define AB3100_OTP5 0xb5
  24. #define AB3100_OTP6 0xb6
  25. #define AB3100_OTP7 0xb7
  26. #define AB3100_OTPP 0xbf
  27. /**
  28. * struct ab3100_otp
  29. * @dev containing device
  30. * @locked whether the OTP is locked, after locking, no more bits
  31. * can be changed but before locking it is still possible
  32. * to change bits from 1->0.
  33. * @freq clocking frequency for the OTP, this frequency is either
  34. * 32768Hz or 1MHz/30
  35. * @paf product activation flag, indicates whether this is a real
  36. * product (paf true) or a lab board etc (paf false)
  37. * @imeich if this is set it is possible to override the
  38. * IMEI number found in the tac, fac and svn fields with
  39. * (secured) software
  40. * @cid customer ID
  41. * @tac type allocation code of the IMEI
  42. * @fac final assembly code of the IMEI
  43. * @svn software version number of the IMEI
  44. * @debugfs a debugfs file used when dumping to file
  45. */
  46. struct ab3100_otp {
  47. struct device *dev;
  48. bool locked;
  49. u32 freq;
  50. bool paf;
  51. bool imeich;
  52. u16 cid:14;
  53. u32 tac:20;
  54. u8 fac;
  55. u32 svn:20;
  56. struct dentry *debugfs;
  57. };
  58. static int __init ab3100_otp_read(struct ab3100_otp *otp)
  59. {
  60. u8 otpval[8];
  61. u8 otpp;
  62. int err;
  63. err = abx500_get_register_interruptible(otp->dev, 0,
  64. AB3100_OTPP, &otpp);
  65. if (err) {
  66. dev_err(otp->dev, "unable to read OTPP register\n");
  67. return err;
  68. }
  69. err = abx500_get_register_page_interruptible(otp->dev, 0,
  70. AB3100_OTP0, otpval, 8);
  71. if (err) {
  72. dev_err(otp->dev, "unable to read OTP register page\n");
  73. return err;
  74. }
  75. /* Cache OTP properties, they never change by nature */
  76. otp->locked = (otpp & 0x80);
  77. otp->freq = (otpp & 0x40) ? 32768 : 34100;
  78. otp->paf = (otpval[1] & 0x80);
  79. otp->imeich = (otpval[1] & 0x40);
  80. otp->cid = ((otpval[1] << 8) | otpval[0]) & 0x3fff;
  81. otp->tac = ((otpval[4] & 0x0f) << 16) | (otpval[3] << 8) | otpval[2];
  82. otp->fac = ((otpval[5] & 0x0f) << 4) | (otpval[4] >> 4);
  83. otp->svn = (otpval[7] << 12) | (otpval[6] << 4) | (otpval[5] >> 4);
  84. return 0;
  85. }
  86. /*
  87. * This is a simple debugfs human-readable file that dumps out
  88. * the contents of the OTP.
  89. */
  90. #ifdef CONFIG_DEBUG_FS
  91. static int ab3100_show_otp(struct seq_file *s, void *v)
  92. {
  93. struct ab3100_otp *otp = s->private;
  94. seq_printf(s, "OTP is %s\n", otp->locked ? "LOCKED" : "UNLOCKED");
  95. seq_printf(s, "OTP clock switch startup is %uHz\n", otp->freq);
  96. seq_printf(s, "PAF is %s\n", otp->paf ? "SET" : "NOT SET");
  97. seq_printf(s, "IMEI is %s\n", otp->imeich ?
  98. "CHANGEABLE" : "NOT CHANGEABLE");
  99. seq_printf(s, "CID: 0x%04x (decimal: %d)\n", otp->cid, otp->cid);
  100. seq_printf(s, "IMEI: %u-%u-%u\n", otp->tac, otp->fac, otp->svn);
  101. return 0;
  102. }
  103. static int ab3100_otp_open(struct inode *inode, struct file *file)
  104. {
  105. return single_open(file, ab3100_show_otp, inode->i_private);
  106. }
  107. static const struct file_operations ab3100_otp_operations = {
  108. .open = ab3100_otp_open,
  109. .read = seq_read,
  110. .llseek = seq_lseek,
  111. .release = single_release,
  112. };
  113. static int __init ab3100_otp_init_debugfs(struct device *dev,
  114. struct ab3100_otp *otp)
  115. {
  116. otp->debugfs = debugfs_create_file("ab3100_otp", S_IFREG | S_IRUGO,
  117. NULL, otp,
  118. &ab3100_otp_operations);
  119. if (!otp->debugfs) {
  120. dev_err(dev, "AB3100 debugfs OTP file registration failed!\n");
  121. return -ENOENT;
  122. }
  123. return 0;
  124. }
  125. static void __exit ab3100_otp_exit_debugfs(struct ab3100_otp *otp)
  126. {
  127. debugfs_remove(otp->debugfs);
  128. }
  129. #else
  130. /* Compile this out if debugfs not selected */
  131. static inline int __init ab3100_otp_init_debugfs(struct device *dev,
  132. struct ab3100_otp *otp)
  133. {
  134. return 0;
  135. }
  136. static inline void __exit ab3100_otp_exit_debugfs(struct ab3100_otp *otp)
  137. {
  138. }
  139. #endif
  140. #define SHOW_AB3100_ATTR(name) \
  141. static ssize_t ab3100_otp_##name##_show(struct device *dev, \
  142. struct device_attribute *attr, \
  143. char *buf) \
  144. {\
  145. struct ab3100_otp *otp = dev_get_drvdata(dev); \
  146. return sprintf(buf, "%u\n", otp->name); \
  147. }
  148. SHOW_AB3100_ATTR(locked)
  149. SHOW_AB3100_ATTR(freq)
  150. SHOW_AB3100_ATTR(paf)
  151. SHOW_AB3100_ATTR(imeich)
  152. SHOW_AB3100_ATTR(cid)
  153. SHOW_AB3100_ATTR(fac)
  154. SHOW_AB3100_ATTR(tac)
  155. SHOW_AB3100_ATTR(svn)
  156. static struct device_attribute ab3100_otp_attrs[] = {
  157. __ATTR(locked, S_IRUGO, ab3100_otp_locked_show, NULL),
  158. __ATTR(freq, S_IRUGO, ab3100_otp_freq_show, NULL),
  159. __ATTR(paf, S_IRUGO, ab3100_otp_paf_show, NULL),
  160. __ATTR(imeich, S_IRUGO, ab3100_otp_imeich_show, NULL),
  161. __ATTR(cid, S_IRUGO, ab3100_otp_cid_show, NULL),
  162. __ATTR(fac, S_IRUGO, ab3100_otp_fac_show, NULL),
  163. __ATTR(tac, S_IRUGO, ab3100_otp_tac_show, NULL),
  164. __ATTR(svn, S_IRUGO, ab3100_otp_svn_show, NULL),
  165. };
  166. static int __init ab3100_otp_probe(struct platform_device *pdev)
  167. {
  168. struct ab3100_otp *otp;
  169. int err = 0;
  170. int i;
  171. otp = devm_kzalloc(&pdev->dev, sizeof(struct ab3100_otp), GFP_KERNEL);
  172. if (!otp)
  173. return -ENOMEM;
  174. otp->dev = &pdev->dev;
  175. /* Replace platform data coming in with a local struct */
  176. platform_set_drvdata(pdev, otp);
  177. err = ab3100_otp_read(otp);
  178. if (err)
  179. return err;
  180. dev_info(&pdev->dev, "AB3100 OTP readout registered\n");
  181. /* sysfs entries */
  182. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++) {
  183. err = device_create_file(&pdev->dev,
  184. &ab3100_otp_attrs[i]);
  185. if (err)
  186. goto err;
  187. }
  188. /* debugfs entries */
  189. err = ab3100_otp_init_debugfs(&pdev->dev, otp);
  190. if (err)
  191. goto err;
  192. return 0;
  193. err:
  194. while (--i >= 0)
  195. device_remove_file(&pdev->dev, &ab3100_otp_attrs[i]);
  196. return err;
  197. }
  198. static int __exit ab3100_otp_remove(struct platform_device *pdev)
  199. {
  200. struct ab3100_otp *otp = platform_get_drvdata(pdev);
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  203. device_remove_file(&pdev->dev,
  204. &ab3100_otp_attrs[i]);
  205. ab3100_otp_exit_debugfs(otp);
  206. return 0;
  207. }
  208. static struct platform_driver ab3100_otp_driver = {
  209. .driver = {
  210. .name = "ab3100-otp",
  211. },
  212. .remove = __exit_p(ab3100_otp_remove),
  213. };
  214. module_platform_driver_probe(ab3100_otp_driver, ab3100_otp_probe);
  215. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  216. MODULE_DESCRIPTION("AB3100 OTP Readout Driver");
  217. MODULE_LICENSE("GPL");