ab3100-otp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mfd/ab3100.h>
  14. #include <linux/debugfs.h>
  15. /* The OTP registers */
  16. #define AB3100_OTP0 0xb0
  17. #define AB3100_OTP1 0xb1
  18. #define AB3100_OTP2 0xb2
  19. #define AB3100_OTP3 0xb3
  20. #define AB3100_OTP4 0xb4
  21. #define AB3100_OTP5 0xb5
  22. #define AB3100_OTP6 0xb6
  23. #define AB3100_OTP7 0xb7
  24. #define AB3100_OTPP 0xbf
  25. /**
  26. * struct ab3100_otp
  27. * @dev containing device
  28. * @ab3100 a pointer to the parent ab3100 device struct
  29. * @locked whether the OTP is locked, after locking, no more bits
  30. * can be changed but before locking it is still possible
  31. * to change bits from 1->0.
  32. * @freq clocking frequency for the OTP, this frequency is either
  33. * 32768Hz or 1MHz/30
  34. * @paf product activation flag, indicates whether this is a real
  35. * product (paf true) or a lab board etc (paf false)
  36. * @imeich if this is set it is possible to override the
  37. * IMEI number found in the tac, fac and svn fields with
  38. * (secured) software
  39. * @cid customer ID
  40. * @tac type allocation code of the IMEI
  41. * @fac final assembly code of the IMEI
  42. * @svn software version number of the IMEI
  43. * @debugfs a debugfs file used when dumping to file
  44. */
  45. struct ab3100_otp {
  46. struct device *dev;
  47. struct ab3100 *ab3100;
  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. struct ab3100 *ab = otp->ab3100;
  61. u8 otpval[8];
  62. u8 otpp;
  63. int err;
  64. err = ab3100_get_register_interruptible(ab, AB3100_OTPP, &otpp);
  65. if (err) {
  66. dev_err(otp->dev, "unable to read OTPP register\n");
  67. return err;
  68. }
  69. err = ab3100_get_register_page_interruptible(ab, AB3100_OTP0,
  70. 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_DEBUGFS
  91. static int show_otp(struct seq_file *s, void *v)
  92. {
  93. struct ab3100_otp *otp = s->private;
  94. int err;
  95. seq_printf(s, "OTP is %s\n", otp->locked ? "LOCKED" : "UNLOCKED");
  96. seq_printf(s, "OTP clock switch startup is %uHz\n", otp->freq);
  97. seq_printf(s, "PAF is %s\n", otp->paf ? "SET" : "NOT SET");
  98. seq_printf(s, "IMEI is %s\n", otp->imeich ?
  99. "CHANGEABLE" : "NOT CHANGEABLE");
  100. seq_printf(s, "CID: 0x%04x (decimal: %d)\n", otp->cid, otp->cid);
  101. seq_printf(s, "IMEI: %u-%u-%u\n", otp->tac, otp->fac, otp->svn);
  102. return 0;
  103. }
  104. static int ab3100_otp_open(struct inode *inode, struct file *file)
  105. {
  106. return single_open(file, ab3100_otp_show, inode->i_private);
  107. }
  108. static const struct file_operations ab3100_otp_operations = {
  109. .open = ab3100_otp_open,
  110. .read = seq_read,
  111. .llseek = seq_lseek,
  112. .release = single_release,
  113. };
  114. static int __init ab3100_otp_init_debugfs(struct device *dev,
  115. struct ab3100_otp *otp)
  116. {
  117. otp->debugfs = debugfs_create_file("ab3100_otp", S_IFREG | S_IRUGO,
  118. NULL, otp,
  119. &ab3100_otp_operations);
  120. if (!otp->debugfs) {
  121. dev_err(dev, "AB3100 debugfs OTP file registration failed!\n");
  122. return err;
  123. }
  124. }
  125. static void __exit ab3100_otp_exit_debugfs(struct ab3100_otp *otp)
  126. {
  127. debugfs_remove_file(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 = kzalloc(sizeof(struct ab3100_otp), GFP_KERNEL);
  172. if (!otp) {
  173. dev_err(&pdev->dev, "could not allocate AB3100 OTP device\n");
  174. return -ENOMEM;
  175. }
  176. otp->dev = &pdev->dev;
  177. /* Replace platform data coming in with a local struct */
  178. otp->ab3100 = platform_get_drvdata(pdev);
  179. platform_set_drvdata(pdev, otp);
  180. err = ab3100_otp_read(otp);
  181. if (err)
  182. return err;
  183. dev_info(&pdev->dev, "AB3100 OTP readout registered\n");
  184. /* sysfs entries */
  185. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++) {
  186. err = device_create_file(&pdev->dev,
  187. &ab3100_otp_attrs[i]);
  188. if (err)
  189. goto out_no_sysfs;
  190. }
  191. /* debugfs entries */
  192. err = ab3100_otp_init_debugfs(&pdev->dev, otp);
  193. if (err)
  194. goto out_no_debugfs;
  195. return 0;
  196. out_no_sysfs:
  197. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  198. device_remove_file(&pdev->dev,
  199. &ab3100_otp_attrs[i]);
  200. out_no_debugfs:
  201. kfree(otp);
  202. return err;
  203. }
  204. static int __exit ab3100_otp_remove(struct platform_device *pdev)
  205. {
  206. struct ab3100_otp *otp = platform_get_drvdata(pdev);
  207. int i;
  208. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  209. device_remove_file(&pdev->dev,
  210. &ab3100_otp_attrs[i]);
  211. ab3100_otp_exit_debugfs(otp);
  212. kfree(otp);
  213. return 0;
  214. }
  215. static struct platform_driver ab3100_otp_driver = {
  216. .driver = {
  217. .name = "ab3100-otp",
  218. .owner = THIS_MODULE,
  219. },
  220. .remove = __exit_p(ab3100_otp_remove),
  221. };
  222. static int __init ab3100_otp_init(void)
  223. {
  224. return platform_driver_probe(&ab3100_otp_driver,
  225. ab3100_otp_probe);
  226. }
  227. static void __exit ab3100_otp_exit(void)
  228. {
  229. platform_driver_unregister(&ab3100_otp_driver);
  230. }
  231. module_init(ab3100_otp_init);
  232. module_exit(ab3100_otp_exit);
  233. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  234. MODULE_DESCRIPTION("AB3100 OTP Readout Driver");
  235. MODULE_LICENSE("GPL");