1/*
2 * I2C access for DA9052 PMICs.
3 *
4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14
15#include <linux/device.h>
16#include <linux/module.h>
17#include <linux/input.h>
18#include <linux/mfd/core.h>
19#include <linux/i2c.h>
20#include <linux/err.h>
21
22#include <linux/mfd/da9052/da9052.h>
23#include <linux/mfd/da9052/reg.h>
24
25#ifdef CONFIG_OF
26#include <linux/of.h>
27#include <linux/of_device.h>
28#endif
29
30/* I2C safe register check */
31static inline bool i2c_safe_reg(unsigned char reg)
32{
33	switch (reg) {
34	case DA9052_STATUS_A_REG:
35	case DA9052_STATUS_B_REG:
36	case DA9052_STATUS_C_REG:
37	case DA9052_STATUS_D_REG:
38	case DA9052_ADC_RES_L_REG:
39	case DA9052_ADC_RES_H_REG:
40	case DA9052_VDD_RES_REG:
41	case DA9052_ICHG_AV_REG:
42	case DA9052_TBAT_RES_REG:
43	case DA9052_ADCIN4_RES_REG:
44	case DA9052_ADCIN5_RES_REG:
45	case DA9052_ADCIN6_RES_REG:
46	case DA9052_TJUNC_RES_REG:
47	case DA9052_TSI_X_MSB_REG:
48	case DA9052_TSI_Y_MSB_REG:
49	case DA9052_TSI_LSB_REG:
50	case DA9052_TSI_Z_MSB_REG:
51		return true;
52	default:
53		return false;
54	}
55}
56
57/*
58 * There is an issue with DA9052 and DA9053_AA/BA/BB PMIC where the PMIC
59 * gets lockup up or fails to respond following a system reset.
60 * This fix is to follow any read or write with a dummy read to a safe
61 * register.
62 */
63static int da9052_i2c_fix(struct da9052 *da9052, unsigned char reg)
64{
65	int val;
66
67	switch (da9052->chip_id) {
68	case DA9052:
69	case DA9053_AA:
70	case DA9053_BA:
71	case DA9053_BB:
72		/* A dummy read to a safe register address. */
73	if (!i2c_safe_reg(reg))
74			return regmap_read(da9052->regmap,
75					   DA9052_PARK_REGISTER,
76					   &val);
77		break;
78	case DA9053_BC:
79	default:
80		/*
81		 * For other chips parking of I2C register
82		 * to a safe place is not required.
83		 */
84		break;
85	}
86
87	return 0;
88}
89
90/*
91 * According to errata item 24, multiwrite mode should be avoided
92 * in order to prevent register data corruption after power-down.
93 */
94static int da9052_i2c_disable_multiwrite(struct da9052 *da9052)
95{
96	int reg_val, ret;
97
98	ret = regmap_read(da9052->regmap, DA9052_CONTROL_B_REG, &reg_val);
99	if (ret < 0)
100		return ret;
101
102	if (!(reg_val & DA9052_CONTROL_B_WRITEMODE)) {
103		reg_val |= DA9052_CONTROL_B_WRITEMODE;
104		ret = regmap_write(da9052->regmap, DA9052_CONTROL_B_REG,
105				   reg_val);
106		if (ret < 0)
107			return ret;
108	}
109
110	return 0;
111}
112
113static const struct i2c_device_id da9052_i2c_id[] = {
114	{"da9052", DA9052},
115	{"da9053-aa", DA9053_AA},
116	{"da9053-ba", DA9053_BA},
117	{"da9053-bb", DA9053_BB},
118	{"da9053-bc", DA9053_BC},
119	{}
120};
121
122#ifdef CONFIG_OF
123static const struct of_device_id dialog_dt_ids[] = {
124	{ .compatible = "dlg,da9052", .data = &da9052_i2c_id[0] },
125	{ .compatible = "dlg,da9053-aa", .data = &da9052_i2c_id[1] },
126	{ .compatible = "dlg,da9053-ba", .data = &da9052_i2c_id[2] },
127	{ .compatible = "dlg,da9053-bb", .data = &da9052_i2c_id[3] },
128	{ .compatible = "dlg,da9053-bc", .data = &da9052_i2c_id[4] },
129	{ /* sentinel */ }
130};
131#endif
132
133static int da9052_i2c_probe(struct i2c_client *client,
134				       const struct i2c_device_id *id)
135{
136	struct da9052 *da9052;
137	int ret;
138
139	da9052 = devm_kzalloc(&client->dev, sizeof(struct da9052), GFP_KERNEL);
140	if (!da9052)
141		return -ENOMEM;
142
143	da9052->dev = &client->dev;
144	da9052->chip_irq = client->irq;
145	da9052->fix_io = da9052_i2c_fix;
146
147	i2c_set_clientdata(client, da9052);
148
149	da9052->regmap = devm_regmap_init_i2c(client, &da9052_regmap_config);
150	if (IS_ERR(da9052->regmap)) {
151		ret = PTR_ERR(da9052->regmap);
152		dev_err(&client->dev, "Failed to allocate register map: %d\n",
153			ret);
154		return ret;
155	}
156
157	ret = da9052_i2c_disable_multiwrite(da9052);
158	if (ret < 0)
159		return ret;
160
161#ifdef CONFIG_OF
162	if (!id) {
163		struct device_node *np = client->dev.of_node;
164		const struct of_device_id *deviceid;
165
166		deviceid = of_match_node(dialog_dt_ids, np);
167		id = deviceid->data;
168	}
169#endif
170
171	if (!id) {
172		ret = -ENODEV;
173		dev_err(&client->dev, "id is null.\n");
174		return ret;
175	}
176
177	return da9052_device_init(da9052, id->driver_data);
178}
179
180static int da9052_i2c_remove(struct i2c_client *client)
181{
182	struct da9052 *da9052 = i2c_get_clientdata(client);
183
184	da9052_device_exit(da9052);
185	return 0;
186}
187
188static struct i2c_driver da9052_i2c_driver = {
189	.probe = da9052_i2c_probe,
190	.remove = da9052_i2c_remove,
191	.id_table = da9052_i2c_id,
192	.driver = {
193		.name = "da9052",
194#ifdef CONFIG_OF
195		.of_match_table = dialog_dt_ids,
196#endif
197	},
198};
199
200static int __init da9052_i2c_init(void)
201{
202	int ret;
203
204	ret = i2c_add_driver(&da9052_i2c_driver);
205	if (ret != 0) {
206		pr_err("DA9052 I2C registration failed %d\n", ret);
207		return ret;
208	}
209
210	return 0;
211}
212subsys_initcall(da9052_i2c_init);
213
214static void __exit da9052_i2c_exit(void)
215{
216	i2c_del_driver(&da9052_i2c_driver);
217}
218module_exit(da9052_i2c_exit);
219
220MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
221MODULE_DESCRIPTION("I2C driver for Dialog DA9052 PMIC");
222MODULE_LICENSE("GPL");
223