blob: 67b12417585d18301170289a50858292e1af807e [file] [log] [blame]
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +01001/*
2 * AS3711 PMIC MFC driver
3 *
4 * Copyright (C) 2012 Renesas Electronics Corporation
5 * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the version 2 of the GNU General Public License as
9 * published by the Free Software Foundation
10 */
11
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/mfd/as3711.h>
18#include <linux/mfd/core.h>
19#include <linux/module.h>
Sachin Kamat0af6f272013-10-16 14:26:48 +053020#include <linux/of.h>
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +010021#include <linux/regmap.h>
22#include <linux/slab.h>
23
24enum {
25 AS3711_REGULATOR,
26 AS3711_BACKLIGHT,
27};
28
29/*
30 * Ok to have it static: it is only used during probing and multiple I2C devices
31 * cannot be probed simultaneously. Just make sure to avoid stale data.
32 */
33static struct mfd_cell as3711_subdevs[] = {
34 [AS3711_REGULATOR] = {.name = "as3711-regulator",},
35 [AS3711_BACKLIGHT] = {.name = "as3711-backlight",},
36};
37
38static bool as3711_volatile_reg(struct device *dev, unsigned int reg)
39{
40 switch (reg) {
41 case AS3711_GPIO_SIGNAL_IN:
42 case AS3711_INTERRUPT_STATUS_1:
43 case AS3711_INTERRUPT_STATUS_2:
44 case AS3711_INTERRUPT_STATUS_3:
45 case AS3711_CHARGER_STATUS_1:
46 case AS3711_CHARGER_STATUS_2:
47 case AS3711_REG_STATUS:
48 return true;
49 }
50 return false;
51}
52
53static bool as3711_precious_reg(struct device *dev, unsigned int reg)
54{
55 switch (reg) {
56 case AS3711_INTERRUPT_STATUS_1:
57 case AS3711_INTERRUPT_STATUS_2:
58 case AS3711_INTERRUPT_STATUS_3:
59 return true;
60 }
61 return false;
62}
63
64static bool as3711_readable_reg(struct device *dev, unsigned int reg)
65{
66 switch (reg) {
67 case AS3711_SD_1_VOLTAGE:
68 case AS3711_SD_2_VOLTAGE:
69 case AS3711_SD_3_VOLTAGE:
70 case AS3711_SD_4_VOLTAGE:
71 case AS3711_LDO_1_VOLTAGE:
72 case AS3711_LDO_2_VOLTAGE:
73 case AS3711_LDO_3_VOLTAGE:
74 case AS3711_LDO_4_VOLTAGE:
75 case AS3711_LDO_5_VOLTAGE:
76 case AS3711_LDO_6_VOLTAGE:
77 case AS3711_LDO_7_VOLTAGE:
78 case AS3711_LDO_8_VOLTAGE:
79 case AS3711_SD_CONTROL:
80 case AS3711_GPIO_SIGNAL_OUT:
81 case AS3711_GPIO_SIGNAL_IN:
82 case AS3711_SD_CONTROL_1:
83 case AS3711_SD_CONTROL_2:
84 case AS3711_CURR_CONTROL:
85 case AS3711_CURR1_VALUE:
86 case AS3711_CURR2_VALUE:
87 case AS3711_CURR3_VALUE:
88 case AS3711_STEPUP_CONTROL_1:
89 case AS3711_STEPUP_CONTROL_2:
90 case AS3711_STEPUP_CONTROL_4:
91 case AS3711_STEPUP_CONTROL_5:
92 case AS3711_REG_STATUS:
93 case AS3711_INTERRUPT_STATUS_1:
94 case AS3711_INTERRUPT_STATUS_2:
95 case AS3711_INTERRUPT_STATUS_3:
96 case AS3711_CHARGER_STATUS_1:
97 case AS3711_CHARGER_STATUS_2:
98 case AS3711_ASIC_ID_1:
99 case AS3711_ASIC_ID_2:
100 return true;
101 }
102 return false;
103}
104
105static const struct regmap_config as3711_regmap_config = {
106 .reg_bits = 8,
107 .val_bits = 8,
108 .volatile_reg = as3711_volatile_reg,
109 .readable_reg = as3711_readable_reg,
110 .precious_reg = as3711_precious_reg,
Maciej S. Szmigieroe9b7ba72016-01-31 22:56:59 +0100111 .max_register = AS3711_MAX_REG,
112 .num_reg_defaults_raw = AS3711_NUM_REGS,
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100113 .cache_type = REGCACHE_RBTREE,
114};
115
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100116#ifdef CONFIG_OF
Krzysztof Kozlowski44560302014-05-13 12:58:50 +0200117static const struct of_device_id as3711_of_match[] = {
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100118 {.compatible = "ams,as3711",},
119 {}
120};
121MODULE_DEVICE_TABLE(of, as3711_of_match);
122#endif
123
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100124static int as3711_i2c_probe(struct i2c_client *client,
125 const struct i2c_device_id *id)
126{
127 struct as3711 *as3711;
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100128 struct as3711_platform_data *pdata;
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100129 unsigned int id1, id2;
130 int ret;
131
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100132 if (!client->dev.of_node) {
Jingoo Han334a41c2013-07-30 17:10:05 +0900133 pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100134 if (!pdata)
135 dev_dbg(&client->dev, "Platform data not found\n");
136 } else {
137 pdata = devm_kzalloc(&client->dev,
138 sizeof(*pdata), GFP_KERNEL);
Lee Jonesae487ae2015-10-28 13:57:44 +0000139 if (!pdata)
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100140 return -ENOMEM;
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100141 }
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100142
143 as3711 = devm_kzalloc(&client->dev, sizeof(struct as3711), GFP_KERNEL);
Lee Jonesae487ae2015-10-28 13:57:44 +0000144 if (!as3711)
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100145 return -ENOMEM;
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100146
147 as3711->dev = &client->dev;
148 i2c_set_clientdata(client, as3711);
149
150 if (client->irq)
151 dev_notice(&client->dev, "IRQ not supported yet\n");
152
153 as3711->regmap = devm_regmap_init_i2c(client, &as3711_regmap_config);
154 if (IS_ERR(as3711->regmap)) {
155 ret = PTR_ERR(as3711->regmap);
Lee Jonesae487ae2015-10-28 13:57:44 +0000156 dev_err(&client->dev,
157 "regmap initialization failed: %d\n", ret);
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100158 return ret;
159 }
160
161 ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_1, &id1);
162 if (!ret)
163 ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_2, &id2);
164 if (ret < 0) {
165 dev_err(&client->dev, "regmap_read() failed: %d\n", ret);
166 return ret;
167 }
168 if (id1 != 0x8b)
169 return -ENODEV;
170 dev_info(as3711->dev, "AS3711 detected: %x:%x\n", id1, id2);
171
Lee Jonesae487ae2015-10-28 13:57:44 +0000172 /*
173 * We can reuse as3711_subdevs[],
174 * it will be copied in mfd_add_devices()
175 */
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100176 if (pdata) {
Lee Jonesae487ae2015-10-28 13:57:44 +0000177 as3711_subdevs[AS3711_REGULATOR].platform_data =
178 &pdata->regulator;
179 as3711_subdevs[AS3711_REGULATOR].pdata_size =
180 sizeof(pdata->regulator);
181 as3711_subdevs[AS3711_BACKLIGHT].platform_data =
182 &pdata->backlight;
183 as3711_subdevs[AS3711_BACKLIGHT].pdata_size =
184 sizeof(pdata->backlight);
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100185 } else {
186 as3711_subdevs[AS3711_REGULATOR].platform_data = NULL;
187 as3711_subdevs[AS3711_REGULATOR].pdata_size = 0;
188 as3711_subdevs[AS3711_BACKLIGHT].platform_data = NULL;
189 as3711_subdevs[AS3711_BACKLIGHT].pdata_size = 0;
190 }
191
Laxman Dewangan9c998322016-04-08 00:12:58 +0530192 ret = devm_mfd_add_devices(as3711->dev, -1, as3711_subdevs,
193 ARRAY_SIZE(as3711_subdevs), NULL, 0, NULL);
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100194 if (ret < 0)
195 dev_err(&client->dev, "add mfd devices failed: %d\n", ret);
196
197 return ret;
198}
199
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100200static const struct i2c_device_id as3711_i2c_id[] = {
201 {.name = "as3711", .driver_data = 0},
202 {}
203};
204
205MODULE_DEVICE_TABLE(i2c, as3711_i2c_id);
206
207static struct i2c_driver as3711_i2c_driver = {
208 .driver = {
209 .name = "as3711",
Guennadi Liakhovetski64710af2013-03-22 17:15:47 +0100210 .of_match_table = of_match_ptr(as3711_of_match),
211 },
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100212 .probe = as3711_i2c_probe,
Guennadi Liakhovetskiacad1892012-11-22 11:12:04 +0100213 .id_table = as3711_i2c_id,
214};
215
216static int __init as3711_i2c_init(void)
217{
218 return i2c_add_driver(&as3711_i2c_driver);
219}
220/* Initialise early */
221subsys_initcall(as3711_i2c_init);
222
223static void __exit as3711_i2c_exit(void)
224{
225 i2c_del_driver(&as3711_i2c_driver);
226}
227module_exit(as3711_i2c_exit);
228
229MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
230MODULE_DESCRIPTION("AS3711 PMIC driver");
231MODULE_LICENSE("GPL v2");