Blame view

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

12992dd89   Linus Walleij   mfd: AB3100 OTP r...
192
193
194
  	otp->dev = &pdev->dev;
  
  	/* Replace platform data coming in with a local struct */
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
195
196
197
198
  	platform_set_drvdata(pdev, otp);
  
  	err = ab3100_otp_read(otp);
  	if (err)
5feac05dd   Lee Jones   mfd: ab3100-otp: ...
199
  		return err;
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
200
201
202
203
204
205
206
207
208
  
  	dev_info(&pdev->dev, "AB3100 OTP readout registered
  ");
  
  	/* sysfs entries */
  	for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++) {
  		err = device_create_file(&pdev->dev,
  					 &ab3100_otp_attrs[i]);
  		if (err)
5feac05dd   Lee Jones   mfd: ab3100-otp: ...
209
  			goto err;
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
210
211
212
  	}
  
  	/* debugfs entries */
45640a383   Greg Kroah-Hartman   mfd: ab3100: no n...
213
  	ab3100_otp_init_debugfs(&pdev->dev, otp);
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
214
215
  
  	return 0;
5feac05dd   Lee Jones   mfd: ab3100-otp: ...
216
  err:
d281b80c4   Axel Lin   mfd: Fix memory l...
217
218
  	while (--i >= 0)
  		device_remove_file(&pdev->dev, &ab3100_otp_attrs[i]);
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
219
220
221
222
223
224
225
226
227
228
229
230
  	return err;
  }
  
  static int __exit ab3100_otp_remove(struct platform_device *pdev)
  {
  	struct ab3100_otp *otp = platform_get_drvdata(pdev);
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(ab3100_otp_attrs); i++)
  		device_remove_file(&pdev->dev,
  				   &ab3100_otp_attrs[i]);
  	ab3100_otp_exit_debugfs(otp);
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
231
232
233
234
235
236
  	return 0;
  }
  
  static struct platform_driver ab3100_otp_driver = {
  	.driver = {
  		.name = "ab3100-otp",
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
237
238
239
  	},
  	.remove	 = __exit_p(ab3100_otp_remove),
  };
bbc48c6aa   Jingoo Han   mfd: ab3100-otp: ...
240
  module_platform_driver_probe(ab3100_otp_driver, ab3100_otp_probe);
12992dd89   Linus Walleij   mfd: AB3100 OTP r...
241
242
243
244
  
  MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  MODULE_DESCRIPTION("AB3100 OTP Readout Driver");
  MODULE_LICENSE("GPL");