Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * linux/drivers/mfd/aat2870-core.c
  3 *
  4 * Copyright (c) 2011, NVIDIA Corporation.
  5 * Author: Jin Park <jinyoungp@nvidia.com>
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * version 2 as published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope that it will be useful, but
 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 * General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 19 * 02110-1301 USA
 20 */
 21
 22#include <linux/kernel.h>
 23#include <linux/module.h>
 24#include <linux/init.h>
 25#include <linux/debugfs.h>
 26#include <linux/slab.h>
 27#include <linux/uaccess.h>
 28#include <linux/i2c.h>
 29#include <linux/delay.h>
 30#include <linux/gpio.h>
 31#include <linux/mfd/core.h>
 32#include <linux/mfd/aat2870.h>
 33#include <linux/regulator/machine.h>
 34
 35static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = {
 36	/* readable, writeable, value */
 37	{ 0, 1, 0x00 },	/* 0x00 AAT2870_BL_CH_EN */
 38	{ 0, 1, 0x16 },	/* 0x01 AAT2870_BLM */
 39	{ 0, 1, 0x16 },	/* 0x02 AAT2870_BLS */
 40	{ 0, 1, 0x56 },	/* 0x03 AAT2870_BL1 */
 41	{ 0, 1, 0x56 },	/* 0x04 AAT2870_BL2 */
 42	{ 0, 1, 0x56 },	/* 0x05 AAT2870_BL3 */
 43	{ 0, 1, 0x56 },	/* 0x06 AAT2870_BL4 */
 44	{ 0, 1, 0x56 },	/* 0x07 AAT2870_BL5 */
 45	{ 0, 1, 0x56 },	/* 0x08 AAT2870_BL6 */
 46	{ 0, 1, 0x56 },	/* 0x09 AAT2870_BL7 */
 47	{ 0, 1, 0x56 },	/* 0x0A AAT2870_BL8 */
 48	{ 0, 1, 0x00 },	/* 0x0B AAT2870_FLR */
 49	{ 0, 1, 0x03 },	/* 0x0C AAT2870_FM */
 50	{ 0, 1, 0x03 },	/* 0x0D AAT2870_FS */
 51	{ 0, 1, 0x10 },	/* 0x0E AAT2870_ALS_CFG0 */
 52	{ 0, 1, 0x06 },	/* 0x0F AAT2870_ALS_CFG1 */
 53	{ 0, 1, 0x00 },	/* 0x10 AAT2870_ALS_CFG2 */
 54	{ 1, 0, 0x00 },	/* 0x11 AAT2870_AMB */
 55	{ 0, 1, 0x00 },	/* 0x12 AAT2870_ALS0 */
 56	{ 0, 1, 0x00 },	/* 0x13 AAT2870_ALS1 */
 57	{ 0, 1, 0x00 },	/* 0x14 AAT2870_ALS2 */
 58	{ 0, 1, 0x00 },	/* 0x15 AAT2870_ALS3 */
 59	{ 0, 1, 0x00 },	/* 0x16 AAT2870_ALS4 */
 60	{ 0, 1, 0x00 },	/* 0x17 AAT2870_ALS5 */
 61	{ 0, 1, 0x00 },	/* 0x18 AAT2870_ALS6 */
 62	{ 0, 1, 0x00 },	/* 0x19 AAT2870_ALS7 */
 63	{ 0, 1, 0x00 },	/* 0x1A AAT2870_ALS8 */
 64	{ 0, 1, 0x00 },	/* 0x1B AAT2870_ALS9 */
 65	{ 0, 1, 0x00 },	/* 0x1C AAT2870_ALSA */
 66	{ 0, 1, 0x00 },	/* 0x1D AAT2870_ALSB */
 67	{ 0, 1, 0x00 },	/* 0x1E AAT2870_ALSC */
 68	{ 0, 1, 0x00 },	/* 0x1F AAT2870_ALSD */
 69	{ 0, 1, 0x00 },	/* 0x20 AAT2870_ALSE */
 70	{ 0, 1, 0x00 },	/* 0x21 AAT2870_ALSF */
 71	{ 0, 1, 0x00 },	/* 0x22 AAT2870_SUB_SET */
 72	{ 0, 1, 0x00 },	/* 0x23 AAT2870_SUB_CTRL */
 73	{ 0, 1, 0x00 },	/* 0x24 AAT2870_LDO_AB */
 74	{ 0, 1, 0x00 },	/* 0x25 AAT2870_LDO_CD */
 75	{ 0, 1, 0x00 },	/* 0x26 AAT2870_LDO_EN */
 76};
 77
 78static struct mfd_cell aat2870_devs[] = {
 79	{
 80		.name = "aat2870-backlight",
 81		.id = AAT2870_ID_BL,
 82		.pdata_size = sizeof(struct aat2870_bl_platform_data),
 83	},
 84	{
 85		.name = "aat2870-regulator",
 86		.id = AAT2870_ID_LDOA,
 87		.pdata_size = sizeof(struct regulator_init_data),
 88	},
 89	{
 90		.name = "aat2870-regulator",
 91		.id = AAT2870_ID_LDOB,
 92		.pdata_size = sizeof(struct regulator_init_data),
 93	},
 94	{
 95		.name = "aat2870-regulator",
 96		.id = AAT2870_ID_LDOC,
 97		.pdata_size = sizeof(struct regulator_init_data),
 98	},
 99	{
100		.name = "aat2870-regulator",
101		.id = AAT2870_ID_LDOD,
102		.pdata_size = sizeof(struct regulator_init_data),
103	},
104};
105
106static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
107{
108	int ret;
109
110	if (addr >= AAT2870_REG_NUM) {
111		dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
112		return -EINVAL;
113	}
114
115	if (!aat2870->reg_cache[addr].readable) {
116		*val = aat2870->reg_cache[addr].value;
117		goto out;
118	}
119
120	ret = i2c_master_send(aat2870->client, &addr, 1);
121	if (ret < 0)
122		return ret;
123	if (ret != 1)
124		return -EIO;
125
126	ret = i2c_master_recv(aat2870->client, val, 1);
127	if (ret < 0)
128		return ret;
129	if (ret != 1)
130		return -EIO;
131
132out:
133	dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val);
134	return 0;
135}
136
137static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
138{
139	u8 msg[2];
140	int ret;
141
142	if (addr >= AAT2870_REG_NUM) {
143		dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
144		return -EINVAL;
145	}
146
147	if (!aat2870->reg_cache[addr].writeable) {
148		dev_err(aat2870->dev, "Address 0x%02x is not writeable\n",
149			addr);
150		return -EINVAL;
151	}
152
153	msg[0] = addr;
154	msg[1] = val;
155	ret = i2c_master_send(aat2870->client, msg, 2);
156	if (ret < 0)
157		return ret;
158	if (ret != 2)
159		return -EIO;
160
161	aat2870->reg_cache[addr].value = val;
162
163	dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val);
164	return 0;
165}
166
167static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
168{
169	int ret;
170
171	mutex_lock(&aat2870->io_lock);
172	ret = __aat2870_read(aat2870, addr, val);
173	mutex_unlock(&aat2870->io_lock);
174
175	return ret;
176}
177
178static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
179{
180	int ret;
181
182	mutex_lock(&aat2870->io_lock);
183	ret = __aat2870_write(aat2870, addr, val);
184	mutex_unlock(&aat2870->io_lock);
185
186	return ret;
187}
188
189static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask,
190			  u8 val)
191{
192	int change;
193	u8 old_val, new_val;
194	int ret;
195
196	mutex_lock(&aat2870->io_lock);
197
198	ret = __aat2870_read(aat2870, addr, &old_val);
199	if (ret)
200		goto out_unlock;
201
202	new_val = (old_val & ~mask) | (val & mask);
203	change = old_val != new_val;
204	if (change)
205		ret = __aat2870_write(aat2870, addr, new_val);
206
207out_unlock:
208	mutex_unlock(&aat2870->io_lock);
209
210	return ret;
211}
212
213static inline void aat2870_enable(struct aat2870_data *aat2870)
214{
215	if (aat2870->en_pin >= 0)
216		gpio_set_value(aat2870->en_pin, 1);
217
218	aat2870->is_enable = 1;
219}
220
221static inline void aat2870_disable(struct aat2870_data *aat2870)
222{
223	if (aat2870->en_pin >= 0)
224		gpio_set_value(aat2870->en_pin, 0);
225
226	aat2870->is_enable = 0;
227}
228
229#ifdef CONFIG_DEBUG_FS
230static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf)
231{
232	u8 addr, val;
233	ssize_t count = 0;
234	int ret;
235
236	count += sprintf(buf, "aat2870 registers\n");
237	for (addr = 0; addr < AAT2870_REG_NUM; addr++) {
238		count += sprintf(buf + count, "0x%02x: ", addr);
239		if (count >= PAGE_SIZE - 1)
240			break;
241
242		ret = aat2870->read(aat2870, addr, &val);
243		if (ret == 0)
244			count += snprintf(buf + count, PAGE_SIZE - count,
245					  "0x%02x", val);
246		else
247			count += snprintf(buf + count, PAGE_SIZE - count,
248					  "<read fail: %d>", ret);
249
250		if (count >= PAGE_SIZE - 1)
251			break;
252
253		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
254		if (count >= PAGE_SIZE - 1)
255			break;
256	}
257
258	/* Truncate count; min() would cause a warning */
259	if (count >= PAGE_SIZE)
260		count = PAGE_SIZE - 1;
261
262	return count;
263}
264
265static int aat2870_reg_open_file(struct inode *inode, struct file *file)
266{
267	file->private_data = inode->i_private;
268
269	return 0;
270}
271
272static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
273				     size_t count, loff_t *ppos)
274{
275	struct aat2870_data *aat2870 = file->private_data;
276	char *buf;
277	ssize_t ret;
278
279	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
280	if (!buf)
281		return -ENOMEM;
282
283	ret = aat2870_dump_reg(aat2870, buf);
284	if (ret >= 0)
285		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
286
287	kfree(buf);
288
289	return ret;
290}
291
292static ssize_t aat2870_reg_write_file(struct file *file,
293				      const char __user *user_buf, size_t count,
294				      loff_t *ppos)
295{
296	struct aat2870_data *aat2870 = file->private_data;
297	char buf[32];
298	int buf_size;
299	char *start = buf;
300	unsigned long addr, val;
301	int ret;
302
303	buf_size = min(count, (sizeof(buf)-1));
304	if (copy_from_user(buf, user_buf, buf_size)) {
305		dev_err(aat2870->dev, "Failed to copy from user\n");
306		return -EFAULT;
307	}
308	buf[buf_size] = 0;
309
310	while (*start == ' ')
311		start++;
312
313	addr = simple_strtoul(start, &start, 16);
 
 
 
314	if (addr >= AAT2870_REG_NUM) {
315		dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
316		return -EINVAL;
317	}
318
319	while (*start == ' ')
320		start++;
321
322	if (strict_strtoul(start, 16, &val))
323		return -EINVAL;
 
324
325	ret = aat2870->write(aat2870, (u8)addr, (u8)val);
326	if (ret)
327		return ret;
328
329	return buf_size;
330}
331
332static const struct file_operations aat2870_reg_fops = {
333	.open = aat2870_reg_open_file,
334	.read = aat2870_reg_read_file,
335	.write = aat2870_reg_write_file,
336};
337
338static void aat2870_init_debugfs(struct aat2870_data *aat2870)
339{
340	aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
341	if (!aat2870->dentry_root) {
342		dev_warn(aat2870->dev,
343			 "Failed to create debugfs root directory\n");
344		return;
345	}
346
347	aat2870->dentry_reg = debugfs_create_file("regs", 0644,
348						  aat2870->dentry_root,
349						  aat2870, &aat2870_reg_fops);
350	if (!aat2870->dentry_reg)
351		dev_warn(aat2870->dev,
352			 "Failed to create debugfs register file\n");
353}
354
355static void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
356{
357	debugfs_remove_recursive(aat2870->dentry_root);
358}
359#else
360static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
361{
362}
363
364static inline void aat2870_uninit_debugfs(struct aat2870_data *aat2870)
365{
366}
367#endif /* CONFIG_DEBUG_FS */
368
369static int aat2870_i2c_probe(struct i2c_client *client,
370			     const struct i2c_device_id *id)
371{
372	struct aat2870_platform_data *pdata = client->dev.platform_data;
373	struct aat2870_data *aat2870;
374	int i, j;
375	int ret = 0;
376
377	aat2870 = kzalloc(sizeof(struct aat2870_data), GFP_KERNEL);
378	if (!aat2870) {
379		dev_err(&client->dev,
380			"Failed to allocate memory for aat2870\n");
381		ret = -ENOMEM;
382		goto out;
383	}
384
385	aat2870->dev = &client->dev;
386	dev_set_drvdata(aat2870->dev, aat2870);
387
388	aat2870->client = client;
389	i2c_set_clientdata(client, aat2870);
390
391	aat2870->reg_cache = aat2870_regs;
392
393	if (pdata->en_pin < 0)
394		aat2870->en_pin = -1;
395	else
396		aat2870->en_pin = pdata->en_pin;
397
398	aat2870->init = pdata->init;
399	aat2870->uninit = pdata->uninit;
400	aat2870->read = aat2870_read;
401	aat2870->write = aat2870_write;
402	aat2870->update = aat2870_update;
403
404	mutex_init(&aat2870->io_lock);
405
406	if (aat2870->init)
407		aat2870->init(aat2870);
408
409	if (aat2870->en_pin >= 0) {
410		ret = gpio_request(aat2870->en_pin, "aat2870-en");
 
411		if (ret < 0) {
412			dev_err(&client->dev,
413				"Failed to request GPIO %d\n", aat2870->en_pin);
414			goto out_kfree;
415		}
416		gpio_direction_output(aat2870->en_pin, 1);
417	}
418
419	aat2870_enable(aat2870);
420
421	for (i = 0; i < pdata->num_subdevs; i++) {
422		for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
423			if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
424					!strcmp(pdata->subdevs[i].name,
425						aat2870_devs[j].name)) {
426				aat2870_devs[j].platform_data =
427					pdata->subdevs[i].platform_data;
428				break;
429			}
430		}
431	}
432
433	ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
434			      ARRAY_SIZE(aat2870_devs), NULL, 0);
435	if (ret != 0) {
436		dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
437		goto out_disable;
438	}
439
440	aat2870_init_debugfs(aat2870);
441
442	return 0;
443
444out_disable:
445	aat2870_disable(aat2870);
446	if (aat2870->en_pin >= 0)
447		gpio_free(aat2870->en_pin);
448out_kfree:
449	kfree(aat2870);
450out:
451	return ret;
452}
453
454static int aat2870_i2c_remove(struct i2c_client *client)
455{
456	struct aat2870_data *aat2870 = i2c_get_clientdata(client);
457
458	aat2870_uninit_debugfs(aat2870);
459
460	mfd_remove_devices(aat2870->dev);
461	aat2870_disable(aat2870);
462	if (aat2870->en_pin >= 0)
463		gpio_free(aat2870->en_pin);
464	if (aat2870->uninit)
465		aat2870->uninit(aat2870);
466	kfree(aat2870);
467
468	return 0;
469}
470
471#ifdef CONFIG_PM
472static int aat2870_i2c_suspend(struct i2c_client *client, pm_message_t state)
473{
 
474	struct aat2870_data *aat2870 = i2c_get_clientdata(client);
475
476	aat2870_disable(aat2870);
477
478	return 0;
479}
480
481static int aat2870_i2c_resume(struct i2c_client *client)
482{
 
483	struct aat2870_data *aat2870 = i2c_get_clientdata(client);
484	struct aat2870_register *reg = NULL;
485	int i;
486
487	aat2870_enable(aat2870);
488
489	/* restore registers */
490	for (i = 0; i < AAT2870_REG_NUM; i++) {
491		reg = &aat2870->reg_cache[i];
492		if (reg->writeable)
493			aat2870->write(aat2870, i, reg->value);
494	}
495
496	return 0;
497}
498#else
499#define aat2870_i2c_suspend	NULL
500#define aat2870_i2c_resume	NULL
501#endif /* CONFIG_PM */
502
503static struct i2c_device_id aat2870_i2c_id_table[] = {
 
 
 
504	{ "aat2870", 0 },
505	{ }
506};
507MODULE_DEVICE_TABLE(i2c, aat2870_i2c_id_table);
508
509static struct i2c_driver aat2870_i2c_driver = {
510	.driver = {
511		.name	= "aat2870",
512		.owner	= THIS_MODULE,
 
513	},
514	.probe		= aat2870_i2c_probe,
515	.remove		= aat2870_i2c_remove,
516	.suspend	= aat2870_i2c_suspend,
517	.resume		= aat2870_i2c_resume,
518	.id_table	= aat2870_i2c_id_table,
519};
520
521static int __init aat2870_init(void)
522{
523	return i2c_add_driver(&aat2870_i2c_driver);
524}
525subsys_initcall(aat2870_init);
526
527static void __exit aat2870_exit(void)
528{
529	i2c_del_driver(&aat2870_i2c_driver);
530}
531module_exit(aat2870_exit);
532
533MODULE_DESCRIPTION("Core support for the AnalogicTech AAT2870");
534MODULE_LICENSE("GPL");
535MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * linux/drivers/mfd/aat2870-core.c
  4 *
  5 * Copyright (c) 2011, NVIDIA Corporation.
  6 * Author: Jin Park <jinyoungp@nvidia.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  7 */
  8
  9#include <linux/kernel.h>
 
 10#include <linux/init.h>
 11#include <linux/debugfs.h>
 12#include <linux/slab.h>
 13#include <linux/uaccess.h>
 14#include <linux/i2c.h>
 15#include <linux/delay.h>
 16#include <linux/gpio.h>
 17#include <linux/mfd/core.h>
 18#include <linux/mfd/aat2870.h>
 19#include <linux/regulator/machine.h>
 20
 21static struct aat2870_register aat2870_regs[AAT2870_REG_NUM] = {
 22	/* readable, writeable, value */
 23	{ 0, 1, 0x00 },	/* 0x00 AAT2870_BL_CH_EN */
 24	{ 0, 1, 0x16 },	/* 0x01 AAT2870_BLM */
 25	{ 0, 1, 0x16 },	/* 0x02 AAT2870_BLS */
 26	{ 0, 1, 0x56 },	/* 0x03 AAT2870_BL1 */
 27	{ 0, 1, 0x56 },	/* 0x04 AAT2870_BL2 */
 28	{ 0, 1, 0x56 },	/* 0x05 AAT2870_BL3 */
 29	{ 0, 1, 0x56 },	/* 0x06 AAT2870_BL4 */
 30	{ 0, 1, 0x56 },	/* 0x07 AAT2870_BL5 */
 31	{ 0, 1, 0x56 },	/* 0x08 AAT2870_BL6 */
 32	{ 0, 1, 0x56 },	/* 0x09 AAT2870_BL7 */
 33	{ 0, 1, 0x56 },	/* 0x0A AAT2870_BL8 */
 34	{ 0, 1, 0x00 },	/* 0x0B AAT2870_FLR */
 35	{ 0, 1, 0x03 },	/* 0x0C AAT2870_FM */
 36	{ 0, 1, 0x03 },	/* 0x0D AAT2870_FS */
 37	{ 0, 1, 0x10 },	/* 0x0E AAT2870_ALS_CFG0 */
 38	{ 0, 1, 0x06 },	/* 0x0F AAT2870_ALS_CFG1 */
 39	{ 0, 1, 0x00 },	/* 0x10 AAT2870_ALS_CFG2 */
 40	{ 1, 0, 0x00 },	/* 0x11 AAT2870_AMB */
 41	{ 0, 1, 0x00 },	/* 0x12 AAT2870_ALS0 */
 42	{ 0, 1, 0x00 },	/* 0x13 AAT2870_ALS1 */
 43	{ 0, 1, 0x00 },	/* 0x14 AAT2870_ALS2 */
 44	{ 0, 1, 0x00 },	/* 0x15 AAT2870_ALS3 */
 45	{ 0, 1, 0x00 },	/* 0x16 AAT2870_ALS4 */
 46	{ 0, 1, 0x00 },	/* 0x17 AAT2870_ALS5 */
 47	{ 0, 1, 0x00 },	/* 0x18 AAT2870_ALS6 */
 48	{ 0, 1, 0x00 },	/* 0x19 AAT2870_ALS7 */
 49	{ 0, 1, 0x00 },	/* 0x1A AAT2870_ALS8 */
 50	{ 0, 1, 0x00 },	/* 0x1B AAT2870_ALS9 */
 51	{ 0, 1, 0x00 },	/* 0x1C AAT2870_ALSA */
 52	{ 0, 1, 0x00 },	/* 0x1D AAT2870_ALSB */
 53	{ 0, 1, 0x00 },	/* 0x1E AAT2870_ALSC */
 54	{ 0, 1, 0x00 },	/* 0x1F AAT2870_ALSD */
 55	{ 0, 1, 0x00 },	/* 0x20 AAT2870_ALSE */
 56	{ 0, 1, 0x00 },	/* 0x21 AAT2870_ALSF */
 57	{ 0, 1, 0x00 },	/* 0x22 AAT2870_SUB_SET */
 58	{ 0, 1, 0x00 },	/* 0x23 AAT2870_SUB_CTRL */
 59	{ 0, 1, 0x00 },	/* 0x24 AAT2870_LDO_AB */
 60	{ 0, 1, 0x00 },	/* 0x25 AAT2870_LDO_CD */
 61	{ 0, 1, 0x00 },	/* 0x26 AAT2870_LDO_EN */
 62};
 63
 64static struct mfd_cell aat2870_devs[] = {
 65	{
 66		.name = "aat2870-backlight",
 67		.id = AAT2870_ID_BL,
 68		.pdata_size = sizeof(struct aat2870_bl_platform_data),
 69	},
 70	{
 71		.name = "aat2870-regulator",
 72		.id = AAT2870_ID_LDOA,
 73		.pdata_size = sizeof(struct regulator_init_data),
 74	},
 75	{
 76		.name = "aat2870-regulator",
 77		.id = AAT2870_ID_LDOB,
 78		.pdata_size = sizeof(struct regulator_init_data),
 79	},
 80	{
 81		.name = "aat2870-regulator",
 82		.id = AAT2870_ID_LDOC,
 83		.pdata_size = sizeof(struct regulator_init_data),
 84	},
 85	{
 86		.name = "aat2870-regulator",
 87		.id = AAT2870_ID_LDOD,
 88		.pdata_size = sizeof(struct regulator_init_data),
 89	},
 90};
 91
 92static int __aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
 93{
 94	int ret;
 95
 96	if (addr >= AAT2870_REG_NUM) {
 97		dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
 98		return -EINVAL;
 99	}
100
101	if (!aat2870->reg_cache[addr].readable) {
102		*val = aat2870->reg_cache[addr].value;
103		goto out;
104	}
105
106	ret = i2c_master_send(aat2870->client, &addr, 1);
107	if (ret < 0)
108		return ret;
109	if (ret != 1)
110		return -EIO;
111
112	ret = i2c_master_recv(aat2870->client, val, 1);
113	if (ret < 0)
114		return ret;
115	if (ret != 1)
116		return -EIO;
117
118out:
119	dev_dbg(aat2870->dev, "read: addr=0x%02x, val=0x%02x\n", addr, *val);
120	return 0;
121}
122
123static int __aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
124{
125	u8 msg[2];
126	int ret;
127
128	if (addr >= AAT2870_REG_NUM) {
129		dev_err(aat2870->dev, "Invalid address, 0x%02x\n", addr);
130		return -EINVAL;
131	}
132
133	if (!aat2870->reg_cache[addr].writeable) {
134		dev_err(aat2870->dev, "Address 0x%02x is not writeable\n",
135			addr);
136		return -EINVAL;
137	}
138
139	msg[0] = addr;
140	msg[1] = val;
141	ret = i2c_master_send(aat2870->client, msg, 2);
142	if (ret < 0)
143		return ret;
144	if (ret != 2)
145		return -EIO;
146
147	aat2870->reg_cache[addr].value = val;
148
149	dev_dbg(aat2870->dev, "write: addr=0x%02x, val=0x%02x\n", addr, val);
150	return 0;
151}
152
153static int aat2870_read(struct aat2870_data *aat2870, u8 addr, u8 *val)
154{
155	int ret;
156
157	mutex_lock(&aat2870->io_lock);
158	ret = __aat2870_read(aat2870, addr, val);
159	mutex_unlock(&aat2870->io_lock);
160
161	return ret;
162}
163
164static int aat2870_write(struct aat2870_data *aat2870, u8 addr, u8 val)
165{
166	int ret;
167
168	mutex_lock(&aat2870->io_lock);
169	ret = __aat2870_write(aat2870, addr, val);
170	mutex_unlock(&aat2870->io_lock);
171
172	return ret;
173}
174
175static int aat2870_update(struct aat2870_data *aat2870, u8 addr, u8 mask,
176			  u8 val)
177{
178	int change;
179	u8 old_val, new_val;
180	int ret;
181
182	mutex_lock(&aat2870->io_lock);
183
184	ret = __aat2870_read(aat2870, addr, &old_val);
185	if (ret)
186		goto out_unlock;
187
188	new_val = (old_val & ~mask) | (val & mask);
189	change = old_val != new_val;
190	if (change)
191		ret = __aat2870_write(aat2870, addr, new_val);
192
193out_unlock:
194	mutex_unlock(&aat2870->io_lock);
195
196	return ret;
197}
198
199static inline void aat2870_enable(struct aat2870_data *aat2870)
200{
201	if (aat2870->en_pin >= 0)
202		gpio_set_value(aat2870->en_pin, 1);
203
204	aat2870->is_enable = 1;
205}
206
207static inline void aat2870_disable(struct aat2870_data *aat2870)
208{
209	if (aat2870->en_pin >= 0)
210		gpio_set_value(aat2870->en_pin, 0);
211
212	aat2870->is_enable = 0;
213}
214
215#ifdef CONFIG_DEBUG_FS
216static ssize_t aat2870_dump_reg(struct aat2870_data *aat2870, char *buf)
217{
218	u8 addr, val;
219	ssize_t count = 0;
220	int ret;
221
222	count += sprintf(buf, "aat2870 registers\n");
223	for (addr = 0; addr < AAT2870_REG_NUM; addr++) {
224		count += snprintf(buf + count, PAGE_SIZE - count, "0x%02x: ", addr);
225		if (count >= PAGE_SIZE - 1)
226			break;
227
228		ret = aat2870->read(aat2870, addr, &val);
229		if (ret == 0)
230			count += snprintf(buf + count, PAGE_SIZE - count,
231					  "0x%02x", val);
232		else
233			count += snprintf(buf + count, PAGE_SIZE - count,
234					  "<read fail: %d>", ret);
235
236		if (count >= PAGE_SIZE - 1)
237			break;
238
239		count += snprintf(buf + count, PAGE_SIZE - count, "\n");
240		if (count >= PAGE_SIZE - 1)
241			break;
242	}
243
244	/* Truncate count; min() would cause a warning */
245	if (count >= PAGE_SIZE)
246		count = PAGE_SIZE - 1;
247
248	return count;
249}
250
 
 
 
 
 
 
 
251static ssize_t aat2870_reg_read_file(struct file *file, char __user *user_buf,
252				     size_t count, loff_t *ppos)
253{
254	struct aat2870_data *aat2870 = file->private_data;
255	char *buf;
256	ssize_t ret;
257
258	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
259	if (!buf)
260		return -ENOMEM;
261
262	ret = aat2870_dump_reg(aat2870, buf);
263	if (ret >= 0)
264		ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
265
266	kfree(buf);
267
268	return ret;
269}
270
271static ssize_t aat2870_reg_write_file(struct file *file,
272				      const char __user *user_buf, size_t count,
273				      loff_t *ppos)
274{
275	struct aat2870_data *aat2870 = file->private_data;
276	char buf[32];
277	ssize_t buf_size;
278	char *start = buf;
279	unsigned long addr, val;
280	int ret;
281
282	buf_size = min(count, (size_t)(sizeof(buf)-1));
283	if (copy_from_user(buf, user_buf, buf_size)) {
284		dev_err(aat2870->dev, "Failed to copy from user\n");
285		return -EFAULT;
286	}
287	buf[buf_size] = 0;
288
289	while (*start == ' ')
290		start++;
291
292	ret = kstrtoul(start, 16, &addr);
293	if (ret)
294		return ret;
295
296	if (addr >= AAT2870_REG_NUM) {
297		dev_err(aat2870->dev, "Invalid address, 0x%lx\n", addr);
298		return -EINVAL;
299	}
300
301	while (*start == ' ')
302		start++;
303
304	ret = kstrtoul(start, 16, &val);
305	if (ret)
306		return ret;
307
308	ret = aat2870->write(aat2870, (u8)addr, (u8)val);
309	if (ret)
310		return ret;
311
312	return buf_size;
313}
314
315static const struct file_operations aat2870_reg_fops = {
316	.open = simple_open,
317	.read = aat2870_reg_read_file,
318	.write = aat2870_reg_write_file,
319};
320
321static void aat2870_init_debugfs(struct aat2870_data *aat2870)
322{
323	aat2870->dentry_root = debugfs_create_dir("aat2870", NULL);
 
 
 
 
 
324
325	debugfs_create_file("regs", 0644, aat2870->dentry_root, aat2870,
326			    &aat2870_reg_fops);
 
 
 
 
327}
328
 
 
 
 
329#else
330static inline void aat2870_init_debugfs(struct aat2870_data *aat2870)
331{
332}
 
 
 
 
333#endif /* CONFIG_DEBUG_FS */
334
335static int aat2870_i2c_probe(struct i2c_client *client,
336			     const struct i2c_device_id *id)
337{
338	struct aat2870_platform_data *pdata = dev_get_platdata(&client->dev);
339	struct aat2870_data *aat2870;
340	int i, j;
341	int ret = 0;
342
343	aat2870 = devm_kzalloc(&client->dev, sizeof(struct aat2870_data),
344				GFP_KERNEL);
345	if (!aat2870)
346		return -ENOMEM;
 
 
 
347
348	aat2870->dev = &client->dev;
349	dev_set_drvdata(aat2870->dev, aat2870);
350
351	aat2870->client = client;
352	i2c_set_clientdata(client, aat2870);
353
354	aat2870->reg_cache = aat2870_regs;
355
356	if (pdata->en_pin < 0)
357		aat2870->en_pin = -1;
358	else
359		aat2870->en_pin = pdata->en_pin;
360
361	aat2870->init = pdata->init;
362	aat2870->uninit = pdata->uninit;
363	aat2870->read = aat2870_read;
364	aat2870->write = aat2870_write;
365	aat2870->update = aat2870_update;
366
367	mutex_init(&aat2870->io_lock);
368
369	if (aat2870->init)
370		aat2870->init(aat2870);
371
372	if (aat2870->en_pin >= 0) {
373		ret = devm_gpio_request_one(&client->dev, aat2870->en_pin,
374					GPIOF_OUT_INIT_HIGH, "aat2870-en");
375		if (ret < 0) {
376			dev_err(&client->dev,
377				"Failed to request GPIO %d\n", aat2870->en_pin);
378			return ret;
379		}
 
380	}
381
382	aat2870_enable(aat2870);
383
384	for (i = 0; i < pdata->num_subdevs; i++) {
385		for (j = 0; j < ARRAY_SIZE(aat2870_devs); j++) {
386			if ((pdata->subdevs[i].id == aat2870_devs[j].id) &&
387					!strcmp(pdata->subdevs[i].name,
388						aat2870_devs[j].name)) {
389				aat2870_devs[j].platform_data =
390					pdata->subdevs[i].platform_data;
391				break;
392			}
393		}
394	}
395
396	ret = mfd_add_devices(aat2870->dev, 0, aat2870_devs,
397			      ARRAY_SIZE(aat2870_devs), NULL, 0, NULL);
398	if (ret != 0) {
399		dev_err(aat2870->dev, "Failed to add subdev: %d\n", ret);
400		goto out_disable;
401	}
402
403	aat2870_init_debugfs(aat2870);
404
405	return 0;
406
407out_disable:
408	aat2870_disable(aat2870);
 
 
 
 
 
409	return ret;
410}
411
412#ifdef CONFIG_PM_SLEEP
413static int aat2870_i2c_suspend(struct device *dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414{
415	struct i2c_client *client = to_i2c_client(dev);
416	struct aat2870_data *aat2870 = i2c_get_clientdata(client);
417
418	aat2870_disable(aat2870);
419
420	return 0;
421}
422
423static int aat2870_i2c_resume(struct device *dev)
424{
425	struct i2c_client *client = to_i2c_client(dev);
426	struct aat2870_data *aat2870 = i2c_get_clientdata(client);
427	struct aat2870_register *reg = NULL;
428	int i;
429
430	aat2870_enable(aat2870);
431
432	/* restore registers */
433	for (i = 0; i < AAT2870_REG_NUM; i++) {
434		reg = &aat2870->reg_cache[i];
435		if (reg->writeable)
436			aat2870->write(aat2870, i, reg->value);
437	}
438
439	return 0;
440}
441#endif /* CONFIG_PM_SLEEP */
 
 
 
442
443static SIMPLE_DEV_PM_OPS(aat2870_pm_ops, aat2870_i2c_suspend,
444			 aat2870_i2c_resume);
445
446static const struct i2c_device_id aat2870_i2c_id_table[] = {
447	{ "aat2870", 0 },
448	{ }
449};
 
450
451static struct i2c_driver aat2870_i2c_driver = {
452	.driver = {
453		.name			= "aat2870",
454		.pm			= &aat2870_pm_ops,
455		.suppress_bind_attrs	= true,
456	},
457	.probe		= aat2870_i2c_probe,
 
 
 
458	.id_table	= aat2870_i2c_id_table,
459};
460
461static int __init aat2870_init(void)
462{
463	return i2c_add_driver(&aat2870_i2c_driver);
464}
465subsys_initcall(aat2870_init);