ab3100-otp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include <linux/seq_file.h>
  16. /* The OTP registers */
  17. #define AB3100_OTP0 0xb0
  18. #define AB3100_OTP1 0xb1
  19. #define AB3100_OTP2 0xb2
  20. #define AB3100_OTP3 0xb3
  21. #define AB3100_OTP4 0xb4
  22. #define AB3100_OTP5 0xb5
  23. #define AB3100_OTP6 0xb6
  24. #define AB3100_OTP7 0xb7
  25. #define AB3100_OTPP 0xbf
  26. /**
  27. * struct ab3100_otp
  28. * @dev containing device
  29. * @ab3100 a pointer to the parent ab3100 device struct
  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. struct ab3100 *ab3100;
  49. bool locked;
  50. u32 freq;
  51. bool paf;
  52. bool imeich;
  53. u16 cid:14;
  54. u32 tac:20;
  55. u8 fac;
  56. u32 svn:20;
  57. struct dentry *debugfs;
  58. };
  59. static int __init ab3100_otp_read(struct ab3100_otp *otp)
  60. {
  61. struct ab3100 *ab = otp->ab3100;
  62. u8 otpval[8];
  63. u8 otpp;
  64. int err;
  65. err = ab3100_get_register_interruptible(ab, AB3100_OTPP, &otpp);
  66. if (err) {
  67. dev_err(otp->dev, "unable to read OTPP register\n");
  68. return err;
  69. }
  70. err = ab3100_get_register_page_interruptible(ab, AB3100_OTP0,
  71. otpval, 8);
  72. if (err) {
  73. dev_err(otp->dev, "unable to read OTP register page\n");
  74. return err;
  75. }
  76. /* Cache OTP properties, they never change by nature */
  77. otp->locked = (otpp & 0x80);
  78. otp->freq = (otpp & 0x40) ? 32768 : 34100;
  79. otp->paf = (otpval[1] & 0x80);
  80. otp->imeich = (otpval[1] & 0x40);
  81. otp->cid = ((otpval[1] << 8) | otpval[0]) & 0x3fff;
  82. otp->tac = ((otpval[4] & 0x0f) << 16) | (otpval[3] << 8) | otpval[2];
  83. otp->fac = ((otpval[5] & 0x0f) << 4) | (otpval[4] >> 4);
  84. otp->svn = (otpval[7] << 12) | (otpval[6] << 4) | (otpval[5] >> 4);
  85. return 0;
  86. }
  87. /*
  88. * This is a simple debugfs human-readable file that dumps out
  89. * the contents of the OTP.
  90. */
  91. #ifdef CONFIG_DEBUG_FS
  92. static int ab3100_show_otp(struct seq_file *s, void *v)
  93. {
  94. struct ab3100_otp *otp = s->private;
  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_show_otp, 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 -ENOENT;
  123. }
  124. return 0;
  125. }
  126. static void __exit ab3100_otp_exit_debugfs(struct ab3100_otp *otp)
  127. {
  128. debugfs_remove(otp->debugfs);
  129. }
  130. #else
  131. /* Compile this out if debugfs not selected */
  132. static inline int __init ab3100_otp_init_debugfs(struct device *dev,
  133. struct ab3100_otp *otp)
  134. {
  135. return 0;
  136. }
  137. static inline void __exit ab3100_otp_exit_debugfs(struct ab3100_otp *otp)
  138. {
  139. }
  140. #endif
  141. #define SHOW_AB3100_ATTR(name) \
  142. static ssize_t ab3100_otp_##name##_show(struct device *dev, \
  143. struct device_attribute *attr, \
  144. char *buf) \
  145. {\
  146. struct ab3100_otp *otp = dev_get_drvdata(dev); \
  147. return sprintf(buf, "%u\n", otp->name); \
  148. }
  149. SHOW_AB3100_ATTR(locked)
  150. SHOW_AB3100_ATTR(freq)
  151. SHOW_AB3100_ATTR(paf)
  152. SHOW_AB3100_ATTR(imeich)
  153. SHOW_AB3100_ATTR(cid)
  154. SHOW_AB3100_ATTR(fac)
  155. SHOW_AB3100_ATTR(tac)
  156. SHOW_AB3100_ATTR(svn)
  157. static struct device_attribute ab3100_otp_attrs[] = {
  158. __ATTR(locked, S_IRUGO, ab3100_otp_locked_show, NULL),
  159. __ATTR(freq, S_IRUGO, ab3100_otp_freq_show, NULL),
  160. __ATTR(paf, S_IRUGO, ab3100_otp_paf_show, NULL),
  161. __ATTR(imeich, S_IRUGO, ab3100_otp_imeich_show, NULL),
  162. __ATTR(cid, S_IRUGO, ab3100_otp_cid_show, NULL),
  163. __ATTR(fac, S_IRUGO, ab3100_otp_fac_show, NULL),
  164. __ATTR(tac, S_IRUGO, ab3100_otp_tac_show, NULL),
  165. __ATTR(svn, S_IRUGO, ab3100_otp_svn_show, NULL),
  166. };
  167. static int __init ab3100_otp_probe(struct platform_device *pdev)
  168. {
  169. struct ab3100_otp *otp;
  170. int err = 0;
  171. int i;
  172. otp = kzalloc(sizeof(struct ab3100_otp), GFP_KERNEL);
  173. if (!otp) {
  174. dev_err(&pdev->dev, "could not allocate AB3100 OTP device\n");
  175. return -ENOMEM;
  176. }
  177. otp->dev = &pdev->dev;
  178. /* Replace platform data coming in with a local struct */
  179. otp->ab3100 = platform_get_drvdata(pdev);
  180. platform_set_drvdata(pdev, otp);
  181. err = ab3100_otp_read(otp);
  182. if (err)
  183. return err;
  184. dev_info(&pdev->dev, "AB3100 OTP readout registered\n");
  185. /* sysfs entries */
  186. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++) {
  187. err = device_create_file(&pdev->dev,
  188. &ab3100_otp_attrs[i]);
  189. if (err)
  190. goto out_no_sysfs;
  191. }
  192. /* debugfs entries */
  193. err = ab3100_otp_init_debugfs(&pdev->dev, otp);
  194. if (err)
  195. goto out_no_debugfs;
  196. return 0;
  197. out_no_sysfs:
  198. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  199. device_remove_file(&pdev->dev,
  200. &ab3100_otp_attrs[i]);
  201. out_no_debugfs:
  202. kfree(otp);
  203. return err;
  204. }
  205. static int __exit ab3100_otp_remove(struct platform_device *pdev)
  206. {
  207. struct ab3100_otp *otp = platform_get_drvdata(pdev);
  208. int i;
  209. for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  210. device_remove_file(&pdev->dev,
  211. &ab3100_otp_attrs[i]);
  212. ab3100_otp_exit_debugfs(otp);
  213. kfree(otp);
  214. return 0;
  215. }
  216. static struct platform_driver ab3100_otp_driver = {
  217. .driver = {
  218. .name = "ab3100-otp",
  219. .owner = THIS_MODULE,
  220. },
  221. .remove = __exit_p(ab3100_otp_remove),
  222. };
  223. static int __init ab3100_otp_init(void)
  224. {
  225. return platform_driver_probe(&ab3100_otp_driver,
  226. ab3100_otp_probe);
  227. }
  228. static void __exit ab3100_otp_exit(void)
  229. {
  230. platform_driver_unregister(&ab3100_otp_driver);
  231. }
  232. module_init(ab3100_otp_init);
  233. module_exit(ab3100_otp_exit);
  234. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  235. MODULE_DESCRIPTION("AB3100 OTP Readout Driver");
  236. MODULE_LICENSE("GPL");