blob: 0f8acc5882a45261ffb5f90cffc23807d90b3412 [file] [log] [blame]
Chris Zhongf69a7cf2014-09-03 21:51:44 +08001/*
Wadim Egorov2eedcbf2016-08-29 13:07:58 +02002 * MFD core driver for Rockchip RK808/RK818
Chris Zhongf69a7cf2014-09-03 21:51:44 +08003 *
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5 *
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
8 *
Wadim Egorov2eedcbf2016-08-29 13:07:58 +02009 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
10 *
11 * Author: Wadim Egorov <w.egorov@phytec.de>
12 *
Chris Zhongf69a7cf2014-09-03 21:51:44 +080013 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 */
22
23#include <linux/i2c.h>
24#include <linux/interrupt.h>
25#include <linux/mfd/rk808.h>
26#include <linux/mfd/core.h>
27#include <linux/module.h>
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020028#include <linux/of_device.h>
Chris Zhongf69a7cf2014-09-03 21:51:44 +080029#include <linux/regmap.h>
30
31struct rk808_reg_data {
32 int addr;
33 int mask;
34 int value;
35};
36
Doug Anderson2adb3b82014-09-09 16:06:04 -070037static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
38{
39 /*
40 * Notes:
41 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
42 * we don't use that feature. It's better to cache.
43 * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
44 * bits are cleared in case when we shutoff anyway, but better safe.
45 */
46
47 switch (reg) {
48 case RK808_SECONDS_REG ... RK808_WEEKS_REG:
49 case RK808_RTC_STATUS_REG:
50 case RK808_VB_MON_REG:
51 case RK808_THERMAL_REG:
52 case RK808_DCDC_UV_STS_REG:
53 case RK808_LDO_UV_STS_REG:
54 case RK808_DCDC_PG_REG:
55 case RK808_LDO_PG_REG:
56 case RK808_DEVCTRL_REG:
57 case RK808_INT_STS_REG1:
58 case RK808_INT_STS_REG2:
59 return true;
60 }
61
62 return false;
63}
64
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020065static const struct regmap_config rk818_regmap_config = {
66 .reg_bits = 8,
67 .val_bits = 8,
68 .max_register = RK818_USB_CTRL_REG,
69 .cache_type = REGCACHE_RBTREE,
70 .volatile_reg = rk808_is_volatile_reg,
71};
72
Chris Zhongf69a7cf2014-09-03 21:51:44 +080073static const struct regmap_config rk808_regmap_config = {
74 .reg_bits = 8,
75 .val_bits = 8,
76 .max_register = RK808_IO_POL_REG,
Doug Anderson2adb3b82014-09-09 16:06:04 -070077 .cache_type = REGCACHE_RBTREE,
78 .volatile_reg = rk808_is_volatile_reg,
Chris Zhongf69a7cf2014-09-03 21:51:44 +080079};
80
81static struct resource rtc_resources[] = {
82 {
83 .start = RK808_IRQ_RTC_ALARM,
84 .end = RK808_IRQ_RTC_ALARM,
85 .flags = IORESOURCE_IRQ,
86 }
87};
88
89static const struct mfd_cell rk808s[] = {
90 { .name = "rk808-clkout", },
91 { .name = "rk808-regulator", },
92 {
93 .name = "rk808-rtc",
94 .num_resources = ARRAY_SIZE(rtc_resources),
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020095 .resources = rtc_resources,
Chris Zhongf69a7cf2014-09-03 21:51:44 +080096 },
97};
98
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020099static const struct mfd_cell rk818s[] = {
100 { .name = "rk808-clkout", },
101 { .name = "rk808-regulator", },
102 {
103 .name = "rk808-rtc",
104 .num_resources = ARRAY_SIZE(rtc_resources),
105 .resources = rtc_resources,
106 },
107};
108
109static const struct rk808_reg_data rk808_pre_init_reg[] = {
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800110 { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
111 { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
112 { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
113 { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
114 { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
Chris Zhonge19f7422015-02-28 18:09:06 +0800115 { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800116 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
117 VB_LO_SEL_3500MV },
118};
119
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200120static const struct rk808_reg_data rk818_pre_init_reg[] = {
121 /* improve efficiency */
122 { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
123 { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
124 { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
125 { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
126 RK818_USB_ILMIN_2000MA },
127 /* close charger when usb lower then 3.4V */
128 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
129 (0x7 << 4) },
130 /* no action when vref */
131 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
132 /* enable HDMI 5V */
133 { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
134 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
135 VB_LO_SEL_3500MV },
136};
137
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800138static const struct regmap_irq rk808_irqs[] = {
139 /* INT_STS */
140 [RK808_IRQ_VOUT_LO] = {
141 .mask = RK808_IRQ_VOUT_LO_MSK,
142 .reg_offset = 0,
143 },
144 [RK808_IRQ_VB_LO] = {
145 .mask = RK808_IRQ_VB_LO_MSK,
146 .reg_offset = 0,
147 },
148 [RK808_IRQ_PWRON] = {
149 .mask = RK808_IRQ_PWRON_MSK,
150 .reg_offset = 0,
151 },
152 [RK808_IRQ_PWRON_LP] = {
153 .mask = RK808_IRQ_PWRON_LP_MSK,
154 .reg_offset = 0,
155 },
156 [RK808_IRQ_HOTDIE] = {
157 .mask = RK808_IRQ_HOTDIE_MSK,
158 .reg_offset = 0,
159 },
160 [RK808_IRQ_RTC_ALARM] = {
161 .mask = RK808_IRQ_RTC_ALARM_MSK,
162 .reg_offset = 0,
163 },
164 [RK808_IRQ_RTC_PERIOD] = {
165 .mask = RK808_IRQ_RTC_PERIOD_MSK,
166 .reg_offset = 0,
167 },
168
169 /* INT_STS2 */
170 [RK808_IRQ_PLUG_IN_INT] = {
171 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
172 .reg_offset = 1,
173 },
174 [RK808_IRQ_PLUG_OUT_INT] = {
175 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
176 .reg_offset = 1,
177 },
178};
179
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200180static const struct regmap_irq rk818_irqs[] = {
181 /* INT_STS */
182 [RK818_IRQ_VOUT_LO] = {
183 .mask = RK818_IRQ_VOUT_LO_MSK,
184 .reg_offset = 0,
185 },
186 [RK818_IRQ_VB_LO] = {
187 .mask = RK818_IRQ_VB_LO_MSK,
188 .reg_offset = 0,
189 },
190 [RK818_IRQ_PWRON] = {
191 .mask = RK818_IRQ_PWRON_MSK,
192 .reg_offset = 0,
193 },
194 [RK818_IRQ_PWRON_LP] = {
195 .mask = RK818_IRQ_PWRON_LP_MSK,
196 .reg_offset = 0,
197 },
198 [RK818_IRQ_HOTDIE] = {
199 .mask = RK818_IRQ_HOTDIE_MSK,
200 .reg_offset = 0,
201 },
202 [RK818_IRQ_RTC_ALARM] = {
203 .mask = RK818_IRQ_RTC_ALARM_MSK,
204 .reg_offset = 0,
205 },
206 [RK818_IRQ_RTC_PERIOD] = {
207 .mask = RK818_IRQ_RTC_PERIOD_MSK,
208 .reg_offset = 0,
209 },
210 [RK818_IRQ_USB_OV] = {
211 .mask = RK818_IRQ_USB_OV_MSK,
212 .reg_offset = 0,
213 },
214
215 /* INT_STS2 */
216 [RK818_IRQ_PLUG_IN] = {
217 .mask = RK818_IRQ_PLUG_IN_MSK,
218 .reg_offset = 1,
219 },
220 [RK818_IRQ_PLUG_OUT] = {
221 .mask = RK818_IRQ_PLUG_OUT_MSK,
222 .reg_offset = 1,
223 },
224 [RK818_IRQ_CHG_OK] = {
225 .mask = RK818_IRQ_CHG_OK_MSK,
226 .reg_offset = 1,
227 },
228 [RK818_IRQ_CHG_TE] = {
229 .mask = RK818_IRQ_CHG_TE_MSK,
230 .reg_offset = 1,
231 },
232 [RK818_IRQ_CHG_TS1] = {
233 .mask = RK818_IRQ_CHG_TS1_MSK,
234 .reg_offset = 1,
235 },
236 [RK818_IRQ_TS2] = {
237 .mask = RK818_IRQ_TS2_MSK,
238 .reg_offset = 1,
239 },
240 [RK818_IRQ_CHG_CVTLIM] = {
241 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
242 .reg_offset = 1,
243 },
244 [RK818_IRQ_DISCHG_ILIM] = {
245 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
246 .reg_offset = 1,
247 },
248};
249
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800250static struct regmap_irq_chip rk808_irq_chip = {
251 .name = "rk808",
252 .irqs = rk808_irqs,
253 .num_irqs = ARRAY_SIZE(rk808_irqs),
254 .num_regs = 2,
255 .irq_reg_stride = 2,
256 .status_base = RK808_INT_STS_REG1,
257 .mask_base = RK808_INT_STS_MSK_REG1,
258 .ack_base = RK808_INT_STS_REG1,
259 .init_ack_masked = true,
260};
261
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200262static struct regmap_irq_chip rk818_irq_chip = {
263 .name = "rk818",
264 .irqs = rk818_irqs,
265 .num_irqs = ARRAY_SIZE(rk818_irqs),
266 .num_regs = 2,
267 .irq_reg_stride = 2,
268 .status_base = RK818_INT_STS_REG1,
269 .mask_base = RK818_INT_STS_MSK_REG1,
270 .ack_base = RK818_INT_STS_REG1,
271 .init_ack_masked = true,
272};
273
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800274static struct i2c_client *rk808_i2c_client;
275static void rk808_device_shutdown(void)
276{
277 int ret;
278 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
279
280 if (!rk808) {
281 dev_warn(&rk808_i2c_client->dev,
282 "have no rk808, so do nothing here\n");
283 return;
284 }
285
286 ret = regmap_update_bits(rk808->regmap,
287 RK808_DEVCTRL_REG,
288 DEV_OFF_RST, DEV_OFF_RST);
289 if (ret)
290 dev_err(&rk808_i2c_client->dev, "power off error!\n");
291}
292
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200293static const struct of_device_id rk808_of_match[] = {
294 { .compatible = "rockchip,rk808" },
295 { .compatible = "rockchip,rk818" },
296 { },
297};
298MODULE_DEVICE_TABLE(of, rk808_of_match);
299
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800300static int rk808_probe(struct i2c_client *client,
301 const struct i2c_device_id *id)
302{
303 struct device_node *np = client->dev.of_node;
304 struct rk808 *rk808;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200305 const struct rk808_reg_data *pre_init_reg;
306 const struct mfd_cell *cells;
307 int nr_pre_init_regs;
308 int nr_cells;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800309 int pm_off = 0;
310 int ret;
311 int i;
312
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200313 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
314 if (!rk808)
315 return -ENOMEM;
316
317 rk808->variant = i2c_smbus_read_word_data(client, RK808_ID_MSB);
318 if (rk808->variant < 0) {
319 dev_err(&client->dev, "Failed to read the chip id at 0x%02x\n",
320 RK808_ID_MSB);
321 return rk808->variant;
322 }
323
324 dev_dbg(&client->dev, "Chip id: 0x%x\n", (unsigned int)rk808->variant);
325
326 switch (rk808->variant) {
327 case RK808_ID:
328 rk808->regmap_cfg = &rk808_regmap_config;
329 rk808->regmap_irq_chip = &rk808_irq_chip;
330 pre_init_reg = rk808_pre_init_reg;
331 nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
332 cells = rk808s;
333 nr_cells = ARRAY_SIZE(rk808s);
334 break;
335 case RK818_ID:
336 rk808->regmap_cfg = &rk818_regmap_config;
337 rk808->regmap_irq_chip = &rk818_irq_chip;
338 pre_init_reg = rk818_pre_init_reg;
339 nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
340 cells = rk818s;
341 nr_cells = ARRAY_SIZE(rk818s);
342 break;
343 default:
344 dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
345 rk808->variant);
346 return -EINVAL;
347 }
348
349 rk808->i2c = client;
350 i2c_set_clientdata(client, rk808);
351
352 rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
353 if (IS_ERR(rk808->regmap)) {
354 dev_err(&client->dev, "regmap initialization failed\n");
355 return PTR_ERR(rk808->regmap);
356 }
357
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800358 if (!client->irq) {
359 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
360 return -EINVAL;
361 }
362
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800363 ret = regmap_add_irq_chip(rk808->regmap, client->irq,
364 IRQF_ONESHOT, -1,
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200365 rk808->regmap_irq_chip, &rk808->irq_data);
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800366 if (ret) {
367 dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
368 return ret;
369 }
370
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200371 for (i = 0; i < nr_pre_init_regs; i++) {
372 ret = regmap_update_bits(rk808->regmap,
373 pre_init_reg[i].addr,
374 pre_init_reg[i].mask,
375 pre_init_reg[i].value);
376 if (ret) {
377 dev_err(&client->dev,
378 "0x%x write err\n",
379 pre_init_reg[i].addr);
380 return ret;
381 }
382 }
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800383
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200384 ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
385 cells, nr_cells, NULL, 0,
386 regmap_irq_get_domain(rk808->irq_data));
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800387 if (ret) {
388 dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
389 goto err_irq;
390 }
391
392 pm_off = of_property_read_bool(np,
393 "rockchip,system-power-controller");
394 if (pm_off && !pm_power_off) {
395 rk808_i2c_client = client;
396 pm_power_off = rk808_device_shutdown;
397 }
398
399 return 0;
400
401err_irq:
402 regmap_del_irq_chip(client->irq, rk808->irq_data);
403 return ret;
404}
405
406static int rk808_remove(struct i2c_client *client)
407{
408 struct rk808 *rk808 = i2c_get_clientdata(client);
409
410 regmap_del_irq_chip(client->irq, rk808->irq_data);
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800411 pm_power_off = NULL;
412
413 return 0;
414}
415
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800416static const struct i2c_device_id rk808_ids[] = {
417 { "rk808" },
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200418 { "rk818" },
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800419 { },
420};
421MODULE_DEVICE_TABLE(i2c, rk808_ids);
422
423static struct i2c_driver rk808_i2c_driver = {
424 .driver = {
425 .name = "rk808",
426 .of_match_table = rk808_of_match,
427 },
428 .probe = rk808_probe,
429 .remove = rk808_remove,
430 .id_table = rk808_ids,
431};
432
433module_i2c_driver(rk808_i2c_driver);
434
435MODULE_LICENSE("GPL");
436MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
437MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200438MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
439MODULE_DESCRIPTION("RK808/RK818 PMIC driver");