1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/debugfs.h>
25#include <linux/slab.h>
26#include <linux/uaccess.h>
27#include <linux/i2c.h>
28#include <linux/delay.h>
29#include <linux/gpio.h>
30#include <linux/mfd/core.h>
31#include <linux/mfd/aat2870.h>
32#include <linux/regulator/machine.h>
33
34static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = {
35
36 { 0, 1, 0x00 },
37 { 0, 1, 0x16 },
38 { 0, 1, 0x16 },
39 { 0, 1, 0x56 },
40 { 0, 1, 0x56 },
41 { 0, 1, 0x56 },
42 { 0, 1, 0x56 },
43 { 0, 1, 0x56 },
44 { 0, 1, 0x56 },
45 { 0, 1, 0x56 },
46 { 0, 1, 0x56 },
47 { 0, 1, 0x00 },
48 { 0, 1, 0x03 },
49 { 0, 1, 0x03 },
50 { 0, 1, 0x10 },
51 { 0, 1, 0x06 },
52 { 0, 1, 0x00 },
53 { 1, 0, 0x00 },
54 { 0, 1, 0x00 },
55 { 0, 1, 0x00 },
56 { 0, 1, 0x00 },
57 { 0, 1, 0x00 },
58 { 0, 1, 0x00 },
59 { 0, 1, 0x00 },
60 { 0, 1, 0x00 },
61 { 0, 1, 0x00 },
62 { 0, 1, 0x00 },
63 { 0, 1, 0x00 },
64 { 0, 1, 0x00 },
65 { 0, 1, 0x00 },
66 { 0, 1, 0x00 },
67 { 0, 1, 0x00 },
68 { 0, 1, 0x00 },
69 { 0, 1, 0x00 },
70 { 0, 1, 0x00 },
71 { 0, 1, 0x00 },
72 { 0, 1, 0x00 },
73 { 0, 1, 0x00 },
74 { 0, 1, 0x00 },
75};
76
77static struct mfd_cell aat2870_devs[] = {
78 {
79 .name = "aat2870-backlight",
80 .id = AAT2870_ID_BL,
81 .pdata_size = sizeof(struct aat2870_bl_platform_data),
82 },
83 {
84 .name = "aat2870-regulator",
85 .id = AAT2870_ID_LDOA,
86 .pdata_size = sizeof(struct regulator_init_data),
87 },
88 {
89 .name = "aat2870-regulator",
90 .id = AAT2870_ID_LDOB,
91 .pdata_size = sizeof(struct regulator_init_data),
92 },
93 {
94 .name = "aat2870-regulator",
95 .id = AAT2870_ID_LDOC,
96 .pdata_size = sizeof(struct regulator_init_data),
97 },
98 {
99 .name = "aat2870-regulator",
100 .id = AAT2870_ID_LDOD,
101 .pdata_size = sizeof(struct regulator_init_data),
102 },
103};
104
105static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
106{
107 int ret;
108
109 if (addr >= AAT2870_REG_NUM) {
110 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
111 return -EINVAL;
112 }
113
114 if (!aat2870->reg_cache[addr].readable) {
115 *val = aat2870->reg_cache[addr].value;
116 goto out;
117 }
118
119 ret = i2c_master_send(aat2870->client, &addr, 1);
120 if (ret < 0)
121 return ret;
122 if (ret != 1)
123 return -EIO;
124
125 ret = i2c_master_recv(aat2870->client, val, 1);
126 if (ret < 0)
127 return ret;
128 if (ret != 1)
129 return -EIO;
130
131out:
132 dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val);
133 return 0;
134}
135
136static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
137{
138 u8 msg[2];
139 int ret;
140
141 if (addr >= AAT2870_REG_NUM) {
142 dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
143 return -EINVAL;
144 }
145
146 if (!aat2870->reg_cache[addr].writeable) {
147 dev_err(aat2870->dev, "Address 0x%02x is not writeable\n",
148 addr);
149 return -EINVAL;
150 }
151
152 msg[0] = addr;
153 msg[1] = val;
154 ret = i2c_master_send(aat2870->client, msg, 2);
155 if (ret < 0)
156 return ret;
157 if (ret != 2)
158 return -EIO;
159
160 aat2870->reg_cache[addr].value = val;
161
162 dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val);
163 return 0;
164}
165
166static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
167{
168 int ret;
169
170 mutex_lock(&aat2870->io_lock);
171 ret = __aat2870_read(aat2870, addr, val);
172 mutex_unlock(&aat2870->io_lock);
173
174 return ret;
175}
176
177static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
178{
179 int ret;
180
181 mutex_lock(&aat2870->io_lock);
182 ret = __aat2870_write(aat2870, addr, val);
183 mutex_unlock(&aat2870->io_lock);
184
185 return ret;
186}
187
188static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask,
189 u8 val)
190{
191 int change;
192 u8 old_val, new_val;
193 int ret;
194
195 mutex_lock(&aat2870->io_lock);
196
197 ret = __aat2870_read(aat2870, addr, &old_val);
198 if (ret)
199 goto out_unlock;
200
201 new_val = (old_val & ~mask) | (val & mask);
202 change = old_val != new_val;
203 if (change)
204 ret = __aat2870_write(aat2870, addr, new_val);
205
206out_unlock:
207 mutex_unlock(&aat2870->io_lock);
208
209 return ret;
210}
211
212static inline void aat2870_enable(struct aat2870_data *aat2870)
213{
214 if (aat2870->en_pin >= 0)
215 gpio_set_value(aat2870->en_pin, 1);
216
217 aat2870->is_enable = 1;
218}
219
220static inline void aat2870_disable(struct aat2870_data *aat2870)
221{
222 if (aat2870->en_pin >= 0)
223 gpio_set_value(aat2870->en_pin, 0);
224
225 aat2870->is_enable = 0;
226}
227
228#ifdef CONFIG_DEBUG_FS
229static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf)
230{
231 u8 addr, val;
232 ssize_t count = 0;
233 int ret;
234
235 count += sprintf(buf, "aat2870 registers\n");
236 for (addr = 0; addr < AAT2870_REG_NUM; addr++) {
237 count += sprintf(buf + count, "0x%02x: ", addr);
238 if (count >= PAGE_SIZE - 1)
239 break;
240
241 ret = aat2870->read(aat2870, addr, &val);
242 if (ret == 0)
243 count += snprintf(buf + count, PAGE_SIZE - count,
244 "0x%02x", val);
245 else
246 count += snprintf(buf + count, PAGE_SIZE - count,
247 "<read fail: %d>", ret);
248
249 if (count >= PAGE_SIZE - 1)
250 break;
251
252 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
253 if (count >= PAGE_SIZE - 1)
254 break;
255 }
256
257
258 if (count >= PAGE_SIZE)
259 count = PAGE_SIZE - 1;
260
261 return count;
262}
263
264static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
265 size_t count, loff_t *ppos)
266{
267 struct aat2870_data *aat2870 = file->private_data;
268 char *buf;
269 ssize_t ret;
270
271 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
272 if (!buf)
273 return -ENOMEM;
274
275 ret = aat2870_dump_reg(aat2870, buf);
276 if (ret >= 0)
277 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
278
279 kfree(buf);
280
281 return ret;
282}
283
284static ssize_t aat2870_reg_write_file(struct file *file,
285 const char __user *user_buf, size_t count,
286 loff_t *ppos)
287{
288 struct aat2870_data *aat2870 = file->private_data;
289 char buf[32];
290 ssize_t buf_size;
291 char *start = buf;
292 unsigned long addr, val;
293 int ret;
294
295 buf_size = min(count, (size_t)(sizeof(buf)-1));
296 if (copy_from_user(buf, user_buf, buf_size)) {
297 dev_err(aat2870->dev, "Failed to copy from user\n");
298 return -EFAULT;
299 }
300 buf[buf_size] = 0;
301
302 while (*start == ' ')
303 start++;
304
305 ret = kstrtoul(start, 16, &addr);
306 if (ret)
307 return ret;
308
309 if (addr >= AAT2870_REG_NUM) {
310 dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
311 return -EINVAL;
312 }
313
314 while (*start == ' ')
315 start++;
316
317 ret = kstrtoul(start, 16, &val);
318 if (ret)
319 return ret;
320
321 ret = aat2870->write(aat2870, (u8)addr, (u8)val);
322 if (ret)
323 return ret;
324
325 return buf_size;
326}
327
328static const struct file_operations aat2870_reg_fops = {
329 .open = simple_open,
330 .read = aat2870_reg_read_file,
331 .write = aat2870_reg_write_file,
332};
333
334static void aat2870_init_debugfs(struct aat2870_data *aat2870)
335{
336 aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
337 if (!aat2870->dentry_root) {
338 dev_warn(aat2870->dev,
339 "Failed to create debugfs root directory\n");
340 return;
341 }
342
343 aat2870->dentry_reg = debugfs_create_file("regs", 0644,
344 aat2870->dentry_root,
345 aat2870, &aat2870_reg_fops);
346 if (!aat2870->dentry_reg)
347 dev_warn(aat2870->dev,
348 "Failed to create debugfs register file\n");
349}
350
351#else
352static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
353{
354}
355#endif
356
357static int aat2870_i2c_probe(struct i2c_client *client,
358 const struct i2c_device_id *id)
359{
360 struct aat2870_platform_data *pdata = dev_get_platdata(&client->dev);
361 struct aat2870_data *aat2870;
362 int i, j;
363 int ret = 0;
364
365 aat2870 = devm_kzalloc(&client->dev, sizeof(struct aat2870_data),
366 GFP_KERNEL);
367 if (!aat2870)
368 return -ENOMEM;
369
370 aat2870->dev = &client->dev;
371 dev_set_drvdata(aat2870->dev, aat2870);
372
373 aat2870->client = client;
374 i2c_set_clientdata(client, aat2870);
375
376 aat2870->reg_cache = aat2870_regs;
377
378 if (pdata->en_pin < 0)
379 aat2870->en_pin = -1;
380 else
381 aat2870->en_pin = pdata->en_pin;
382
383 aat2870->init = pdata->init;
384 aat2870->uninit = pdata->uninit;
385 aat2870->read = aat2870_read;
386 aat2870->write = aat2870_write;
387 aat2870->update = aat2870_update;
388
389 mutex_init(&aat2870->io_lock);
390
391 if (aat2870->init)
392 aat2870->init(aat2870);
393
394 if (aat2870->en_pin >= 0) {
395 ret = devm_gpio_request_one(&client->dev, aat2870->en_pin,
396 GPIOF_OUT_INIT_HIGH, "aat2870-en");
397 if (ret < 0) {
398 dev_err(&client->dev,
399 "Failed to request GPIO %d\n", aat2870->en_pin);
400 return ret;
401 }
402 }
403
404 aat2870_enable(aat2870);
405
406 for (i = 0; i < pdata->num_subdevs; i++) {
407 for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
408 if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
409 !strcmp(pdata->subdevs[i].name,
410 aat2870_devs[j].name)) {
411 aat2870_devs[j].platform_data =
412 pdata->subdevs[i].platform_data;
413 break;
414 }
415 }
416 }
417
418 ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
419 ARRAY_SIZE(aat2870_devs), NULL, 0, NULL);
420 if (ret != 0) {
421 dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
422 goto out_disable;
423 }
424
425 aat2870_init_debugfs(aat2870);
426
427 return 0;
428
429out_disable:
430 aat2870_disable(aat2870);
431 return ret;
432}
433
434#ifdef CONFIG_PM_SLEEP
435static int aat2870_i2c_suspend(struct device *dev)
436{
437 struct i2c_client *client = to_i2c_client(dev);
438 struct aat2870_data *aat2870 = i2c_get_clientdata(client);
439
440 aat2870_disable(aat2870);
441
442 return 0;
443}
444
445static int aat2870_i2c_resume(struct device *dev)
446{
447 struct i2c_client *client = to_i2c_client(dev);
448 struct aat2870_data *aat2870 = i2c_get_clientdata(client);
449 struct aat2870_register *reg = NULL;
450 int i;
451
452 aat2870_enable(aat2870);
453
454
455 for (i = 0; i < AAT2870_REG_NUM; i++) {
456 reg = &aat2870->reg_cache[i];
457 if (reg->writeable)
458 aat2870->write(aat2870, i, reg->value);
459 }
460
461 return 0;
462}
463#endif
464
465static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend,
466 aat2870_i2c_resume);
467
468static const struct i2c_device_id aat2870_i2c_id_table[] = {
469 { "aat2870", 0 },
470 { }
471};
472
473static struct i2c_driver aat2870_i2c_driver = {
474 .driver = {
475 .name = "aat2870",
476 .pm = &aat2870_pm_ops,
477 .suppress_bind_attrs = true,
478 },
479 .probe = aat2870_i2c_probe,
480 .id_table = aat2870_i2c_id_table,
481};
482
483static int __init aat2870_init(void)
484{
485 return i2c_add_driver(&aat2870_i2c_driver);
486}
487subsys_initcall(aat2870_init);
488