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