ab3100-otp.c 7.0 KB

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