KVM: arm64: Fix order of vcpu_write_sys_reg() arguments
[linux/fpc-iii.git] / drivers / mfd / rk808.c
blob216fbf6adec9906e9c49991929aaa07f4ec0f1e5
1 /*
2 * MFD core driver for Rockchip RK808/RK818
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
9 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11 * Author: Wadim Egorov <w.egorov@phytec.de>
13 * 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.
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.
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>
28 #include <linux/of_device.h>
29 #include <linux/regmap.h>
31 struct rk808_reg_data {
32 int addr;
33 int mask;
34 int value;
37 static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
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.
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;
62 return false;
65 static 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,
73 static const struct regmap_config rk805_regmap_config = {
74 .reg_bits = 8,
75 .val_bits = 8,
76 .max_register = RK805_OFF_SOURCE_REG,
77 .cache_type = REGCACHE_RBTREE,
78 .volatile_reg = rk808_is_volatile_reg,
81 static const struct regmap_config rk808_regmap_config = {
82 .reg_bits = 8,
83 .val_bits = 8,
84 .max_register = RK808_IO_POL_REG,
85 .cache_type = REGCACHE_RBTREE,
86 .volatile_reg = rk808_is_volatile_reg,
89 static struct resource rtc_resources[] = {
91 .start = RK808_IRQ_RTC_ALARM,
92 .end = RK808_IRQ_RTC_ALARM,
93 .flags = IORESOURCE_IRQ,
97 static struct resource rk805_key_resources[] = {
99 .start = RK805_IRQ_PWRON_FALL,
100 .end = RK805_IRQ_PWRON_FALL,
101 .flags = IORESOURCE_IRQ,
104 .start = RK805_IRQ_PWRON_RISE,
105 .end = RK805_IRQ_PWRON_RISE,
106 .flags = IORESOURCE_IRQ,
110 static const struct mfd_cell rk805s[] = {
111 { .name = "rk808-clkout", },
112 { .name = "rk808-regulator", },
113 { .name = "rk805-pinctrl", },
115 .name = "rk808-rtc",
116 .num_resources = ARRAY_SIZE(rtc_resources),
117 .resources = &rtc_resources[0],
119 { .name = "rk805-pwrkey",
120 .num_resources = ARRAY_SIZE(rk805_key_resources),
121 .resources = &rk805_key_resources[0],
125 static const struct mfd_cell rk808s[] = {
126 { .name = "rk808-clkout", },
127 { .name = "rk808-regulator", },
129 .name = "rk808-rtc",
130 .num_resources = ARRAY_SIZE(rtc_resources),
131 .resources = rtc_resources,
135 static const struct mfd_cell rk818s[] = {
136 { .name = "rk808-clkout", },
137 { .name = "rk808-regulator", },
139 .name = "rk808-rtc",
140 .num_resources = ARRAY_SIZE(rtc_resources),
141 .resources = rtc_resources,
145 static const struct rk808_reg_data rk805_pre_init_reg[] = {
146 {RK805_BUCK1_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
147 RK805_BUCK1_2_ILMAX_4000MA},
148 {RK805_BUCK2_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
149 RK805_BUCK1_2_ILMAX_4000MA},
150 {RK805_BUCK3_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
151 RK805_BUCK3_ILMAX_3000MA},
152 {RK805_BUCK4_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
153 RK805_BUCK4_ILMAX_3500MA},
154 {RK805_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_400MA},
155 {RK805_GPIO_IO_POL_REG, SLP_SD_MSK, SLEEP_FUN},
156 {RK805_THERMAL_REG, TEMP_HOTDIE_MSK, TEMP115C},
159 static const struct rk808_reg_data rk808_pre_init_reg[] = {
160 { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
161 { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
162 { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
163 { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
164 { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
165 { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
166 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
167 VB_LO_SEL_3500MV },
170 static const struct rk808_reg_data rk818_pre_init_reg[] = {
171 /* improve efficiency */
172 { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
173 { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
174 { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
175 { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
176 RK818_USB_ILMIN_2000MA },
177 /* close charger when usb lower then 3.4V */
178 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
179 (0x7 << 4) },
180 /* no action when vref */
181 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
182 /* enable HDMI 5V */
183 { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
184 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
185 VB_LO_SEL_3500MV },
188 static const struct regmap_irq rk805_irqs[] = {
189 [RK805_IRQ_PWRON_RISE] = {
190 .mask = RK805_IRQ_PWRON_RISE_MSK,
191 .reg_offset = 0,
193 [RK805_IRQ_VB_LOW] = {
194 .mask = RK805_IRQ_VB_LOW_MSK,
195 .reg_offset = 0,
197 [RK805_IRQ_PWRON] = {
198 .mask = RK805_IRQ_PWRON_MSK,
199 .reg_offset = 0,
201 [RK805_IRQ_PWRON_LP] = {
202 .mask = RK805_IRQ_PWRON_LP_MSK,
203 .reg_offset = 0,
205 [RK805_IRQ_HOTDIE] = {
206 .mask = RK805_IRQ_HOTDIE_MSK,
207 .reg_offset = 0,
209 [RK805_IRQ_RTC_ALARM] = {
210 .mask = RK805_IRQ_RTC_ALARM_MSK,
211 .reg_offset = 0,
213 [RK805_IRQ_RTC_PERIOD] = {
214 .mask = RK805_IRQ_RTC_PERIOD_MSK,
215 .reg_offset = 0,
217 [RK805_IRQ_PWRON_FALL] = {
218 .mask = RK805_IRQ_PWRON_FALL_MSK,
219 .reg_offset = 0,
223 static const struct regmap_irq rk808_irqs[] = {
224 /* INT_STS */
225 [RK808_IRQ_VOUT_LO] = {
226 .mask = RK808_IRQ_VOUT_LO_MSK,
227 .reg_offset = 0,
229 [RK808_IRQ_VB_LO] = {
230 .mask = RK808_IRQ_VB_LO_MSK,
231 .reg_offset = 0,
233 [RK808_IRQ_PWRON] = {
234 .mask = RK808_IRQ_PWRON_MSK,
235 .reg_offset = 0,
237 [RK808_IRQ_PWRON_LP] = {
238 .mask = RK808_IRQ_PWRON_LP_MSK,
239 .reg_offset = 0,
241 [RK808_IRQ_HOTDIE] = {
242 .mask = RK808_IRQ_HOTDIE_MSK,
243 .reg_offset = 0,
245 [RK808_IRQ_RTC_ALARM] = {
246 .mask = RK808_IRQ_RTC_ALARM_MSK,
247 .reg_offset = 0,
249 [RK808_IRQ_RTC_PERIOD] = {
250 .mask = RK808_IRQ_RTC_PERIOD_MSK,
251 .reg_offset = 0,
254 /* INT_STS2 */
255 [RK808_IRQ_PLUG_IN_INT] = {
256 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
257 .reg_offset = 1,
259 [RK808_IRQ_PLUG_OUT_INT] = {
260 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
261 .reg_offset = 1,
265 static const struct regmap_irq rk818_irqs[] = {
266 /* INT_STS */
267 [RK818_IRQ_VOUT_LO] = {
268 .mask = RK818_IRQ_VOUT_LO_MSK,
269 .reg_offset = 0,
271 [RK818_IRQ_VB_LO] = {
272 .mask = RK818_IRQ_VB_LO_MSK,
273 .reg_offset = 0,
275 [RK818_IRQ_PWRON] = {
276 .mask = RK818_IRQ_PWRON_MSK,
277 .reg_offset = 0,
279 [RK818_IRQ_PWRON_LP] = {
280 .mask = RK818_IRQ_PWRON_LP_MSK,
281 .reg_offset = 0,
283 [RK818_IRQ_HOTDIE] = {
284 .mask = RK818_IRQ_HOTDIE_MSK,
285 .reg_offset = 0,
287 [RK818_IRQ_RTC_ALARM] = {
288 .mask = RK818_IRQ_RTC_ALARM_MSK,
289 .reg_offset = 0,
291 [RK818_IRQ_RTC_PERIOD] = {
292 .mask = RK818_IRQ_RTC_PERIOD_MSK,
293 .reg_offset = 0,
295 [RK818_IRQ_USB_OV] = {
296 .mask = RK818_IRQ_USB_OV_MSK,
297 .reg_offset = 0,
300 /* INT_STS2 */
301 [RK818_IRQ_PLUG_IN] = {
302 .mask = RK818_IRQ_PLUG_IN_MSK,
303 .reg_offset = 1,
305 [RK818_IRQ_PLUG_OUT] = {
306 .mask = RK818_IRQ_PLUG_OUT_MSK,
307 .reg_offset = 1,
309 [RK818_IRQ_CHG_OK] = {
310 .mask = RK818_IRQ_CHG_OK_MSK,
311 .reg_offset = 1,
313 [RK818_IRQ_CHG_TE] = {
314 .mask = RK818_IRQ_CHG_TE_MSK,
315 .reg_offset = 1,
317 [RK818_IRQ_CHG_TS1] = {
318 .mask = RK818_IRQ_CHG_TS1_MSK,
319 .reg_offset = 1,
321 [RK818_IRQ_TS2] = {
322 .mask = RK818_IRQ_TS2_MSK,
323 .reg_offset = 1,
325 [RK818_IRQ_CHG_CVTLIM] = {
326 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
327 .reg_offset = 1,
329 [RK818_IRQ_DISCHG_ILIM] = {
330 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
331 .reg_offset = 1,
335 static struct regmap_irq_chip rk805_irq_chip = {
336 .name = "rk805",
337 .irqs = rk805_irqs,
338 .num_irqs = ARRAY_SIZE(rk805_irqs),
339 .num_regs = 1,
340 .status_base = RK805_INT_STS_REG,
341 .mask_base = RK805_INT_STS_MSK_REG,
342 .ack_base = RK805_INT_STS_REG,
343 .init_ack_masked = true,
346 static const struct regmap_irq_chip rk808_irq_chip = {
347 .name = "rk808",
348 .irqs = rk808_irqs,
349 .num_irqs = ARRAY_SIZE(rk808_irqs),
350 .num_regs = 2,
351 .irq_reg_stride = 2,
352 .status_base = RK808_INT_STS_REG1,
353 .mask_base = RK808_INT_STS_MSK_REG1,
354 .ack_base = RK808_INT_STS_REG1,
355 .init_ack_masked = true,
358 static const struct regmap_irq_chip rk818_irq_chip = {
359 .name = "rk818",
360 .irqs = rk818_irqs,
361 .num_irqs = ARRAY_SIZE(rk818_irqs),
362 .num_regs = 2,
363 .irq_reg_stride = 2,
364 .status_base = RK818_INT_STS_REG1,
365 .mask_base = RK818_INT_STS_MSK_REG1,
366 .ack_base = RK818_INT_STS_REG1,
367 .init_ack_masked = true,
370 static struct i2c_client *rk808_i2c_client;
372 static void rk805_device_shutdown(void)
374 int ret;
375 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
377 if (!rk808) {
378 dev_warn(&rk808_i2c_client->dev,
379 "have no rk805, so do nothing here\n");
380 return;
383 ret = regmap_update_bits(rk808->regmap,
384 RK805_DEV_CTRL_REG,
385 DEV_OFF, DEV_OFF);
386 if (ret)
387 dev_err(&rk808_i2c_client->dev, "power off error!\n");
390 static void rk808_device_shutdown(void)
392 int ret;
393 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
395 if (!rk808) {
396 dev_warn(&rk808_i2c_client->dev,
397 "have no rk808, so do nothing here\n");
398 return;
401 ret = regmap_update_bits(rk808->regmap,
402 RK808_DEVCTRL_REG,
403 DEV_OFF_RST, DEV_OFF_RST);
404 if (ret)
405 dev_err(&rk808_i2c_client->dev, "power off error!\n");
408 static void rk818_device_shutdown(void)
410 int ret;
411 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
413 if (!rk808) {
414 dev_warn(&rk808_i2c_client->dev,
415 "have no rk818, so do nothing here\n");
416 return;
419 ret = regmap_update_bits(rk808->regmap,
420 RK818_DEVCTRL_REG,
421 DEV_OFF, DEV_OFF);
422 if (ret)
423 dev_err(&rk808_i2c_client->dev, "power off error!\n");
426 static const struct of_device_id rk808_of_match[] = {
427 { .compatible = "rockchip,rk805" },
428 { .compatible = "rockchip,rk808" },
429 { .compatible = "rockchip,rk818" },
430 { },
432 MODULE_DEVICE_TABLE(of, rk808_of_match);
434 static int rk808_probe(struct i2c_client *client,
435 const struct i2c_device_id *id)
437 struct device_node *np = client->dev.of_node;
438 struct rk808 *rk808;
439 const struct rk808_reg_data *pre_init_reg;
440 const struct mfd_cell *cells;
441 void (*pm_pwroff_fn)(void);
442 int nr_pre_init_regs;
443 int nr_cells;
444 int pm_off = 0, msb, lsb;
445 int ret;
446 int i;
448 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
449 if (!rk808)
450 return -ENOMEM;
452 /* Read chip variant */
453 msb = i2c_smbus_read_byte_data(client, RK808_ID_MSB);
454 if (msb < 0) {
455 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
456 RK808_ID_MSB);
457 return msb;
460 lsb = i2c_smbus_read_byte_data(client, RK808_ID_LSB);
461 if (lsb < 0) {
462 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
463 RK808_ID_LSB);
464 return lsb;
467 rk808->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
468 dev_info(&client->dev, "chip id: 0x%x\n", (unsigned int)rk808->variant);
470 switch (rk808->variant) {
471 case RK805_ID:
472 rk808->regmap_cfg = &rk805_regmap_config;
473 rk808->regmap_irq_chip = &rk805_irq_chip;
474 pre_init_reg = rk805_pre_init_reg;
475 nr_pre_init_regs = ARRAY_SIZE(rk805_pre_init_reg);
476 cells = rk805s;
477 nr_cells = ARRAY_SIZE(rk805s);
478 pm_pwroff_fn = rk805_device_shutdown;
479 break;
480 case RK808_ID:
481 rk808->regmap_cfg = &rk808_regmap_config;
482 rk808->regmap_irq_chip = &rk808_irq_chip;
483 pre_init_reg = rk808_pre_init_reg;
484 nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
485 cells = rk808s;
486 nr_cells = ARRAY_SIZE(rk808s);
487 pm_pwroff_fn = rk808_device_shutdown;
488 break;
489 case RK818_ID:
490 rk808->regmap_cfg = &rk818_regmap_config;
491 rk808->regmap_irq_chip = &rk818_irq_chip;
492 pre_init_reg = rk818_pre_init_reg;
493 nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
494 cells = rk818s;
495 nr_cells = ARRAY_SIZE(rk818s);
496 pm_pwroff_fn = rk818_device_shutdown;
497 break;
498 default:
499 dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
500 rk808->variant);
501 return -EINVAL;
504 rk808->i2c = client;
505 i2c_set_clientdata(client, rk808);
507 rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
508 if (IS_ERR(rk808->regmap)) {
509 dev_err(&client->dev, "regmap initialization failed\n");
510 return PTR_ERR(rk808->regmap);
513 if (!client->irq) {
514 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
515 return -EINVAL;
518 ret = regmap_add_irq_chip(rk808->regmap, client->irq,
519 IRQF_ONESHOT, -1,
520 rk808->regmap_irq_chip, &rk808->irq_data);
521 if (ret) {
522 dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
523 return ret;
526 for (i = 0; i < nr_pre_init_regs; i++) {
527 ret = regmap_update_bits(rk808->regmap,
528 pre_init_reg[i].addr,
529 pre_init_reg[i].mask,
530 pre_init_reg[i].value);
531 if (ret) {
532 dev_err(&client->dev,
533 "0x%x write err\n",
534 pre_init_reg[i].addr);
535 return ret;
539 ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
540 cells, nr_cells, NULL, 0,
541 regmap_irq_get_domain(rk808->irq_data));
542 if (ret) {
543 dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
544 goto err_irq;
547 pm_off = of_property_read_bool(np,
548 "rockchip,system-power-controller");
549 if (pm_off && !pm_power_off) {
550 rk808_i2c_client = client;
551 pm_power_off = pm_pwroff_fn;
554 return 0;
556 err_irq:
557 regmap_del_irq_chip(client->irq, rk808->irq_data);
558 return ret;
561 static int rk808_remove(struct i2c_client *client)
563 struct rk808 *rk808 = i2c_get_clientdata(client);
565 regmap_del_irq_chip(client->irq, rk808->irq_data);
566 pm_power_off = NULL;
568 return 0;
571 static const struct i2c_device_id rk808_ids[] = {
572 { "rk805" },
573 { "rk808" },
574 { "rk818" },
575 { },
577 MODULE_DEVICE_TABLE(i2c, rk808_ids);
579 static struct i2c_driver rk808_i2c_driver = {
580 .driver = {
581 .name = "rk808",
582 .of_match_table = rk808_of_match,
584 .probe = rk808_probe,
585 .remove = rk808_remove,
586 .id_table = rk808_ids,
589 module_i2c_driver(rk808_i2c_driver);
591 MODULE_LICENSE("GPL");
592 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
593 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
594 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
595 MODULE_DESCRIPTION("RK808/RK818 PMIC driver");