1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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>
28#include <linux/of_device.h>
29#include <linux/regmap.h>
30
31struct rk808_reg_data {
32 int addr;
33 int mask;
34 int value;
35};
36
37static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
38{
39
40
41
42
43
44
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
65static 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
73static 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,
79};
80
81static 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,
87};
88
89static struct resource rtc_resources[] = {
90 {
91 .start = RK808_IRQ_RTC_ALARM,
92 .end = RK808_IRQ_RTC_ALARM,
93 .flags = IORESOURCE_IRQ,
94 }
95};
96
97static struct resource rk805_key_resources[] = {
98 {
99 .start = RK805_IRQ_PWRON_FALL,
100 .end = RK805_IRQ_PWRON_FALL,
101 .flags = IORESOURCE_IRQ,
102 },
103 {
104 .start = RK805_IRQ_PWRON_RISE,
105 .end = RK805_IRQ_PWRON_RISE,
106 .flags = IORESOURCE_IRQ,
107 }
108};
109
110static const struct mfd_cell rk805s[] = {
111 { .name = "rk808-clkout", },
112 { .name = "rk808-regulator", },
113 { .name = "rk805-pinctrl", },
114 {
115 .name = "rk808-rtc",
116 .num_resources = ARRAY_SIZE(rtc_resources),
117 .resources = &rtc_resources[0],
118 },
119 { .name = "rk805-pwrkey",
120 .num_resources = ARRAY_SIZE(rk805_key_resources),
121 .resources = &rk805_key_resources[0],
122 },
123};
124
125static const struct mfd_cell rk808s[] = {
126 { .name = "rk808-clkout", },
127 { .name = "rk808-regulator", },
128 {
129 .name = "rk808-rtc",
130 .num_resources = ARRAY_SIZE(rtc_resources),
131 .resources = rtc_resources,
132 },
133};
134
135static const struct mfd_cell rk818s[] = {
136 { .name = "rk808-clkout", },
137 { .name = "rk808-regulator", },
138 {
139 .name = "rk808-rtc",
140 .num_resources = ARRAY_SIZE(rtc_resources),
141 .resources = rtc_resources,
142 },
143};
144
145static 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},
157};
158
159static 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 },
168};
169
170static const struct rk808_reg_data rk818_pre_init_reg[] = {
171
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
178 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
179 (0x7 << 4) },
180
181 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
182
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 },
186};
187
188static const struct regmap_irq rk805_irqs[] = {
189 [RK805_IRQ_PWRON_RISE] = {
190 .mask = RK805_IRQ_PWRON_RISE_MSK,
191 .reg_offset = 0,
192 },
193 [RK805_IRQ_VB_LOW] = {
194 .mask = RK805_IRQ_VB_LOW_MSK,
195 .reg_offset = 0,
196 },
197 [RK805_IRQ_PWRON] = {
198 .mask = RK805_IRQ_PWRON_MSK,
199 .reg_offset = 0,
200 },
201 [RK805_IRQ_PWRON_LP] = {
202 .mask = RK805_IRQ_PWRON_LP_MSK,
203 .reg_offset = 0,
204 },
205 [RK805_IRQ_HOTDIE] = {
206 .mask = RK805_IRQ_HOTDIE_MSK,
207 .reg_offset = 0,
208 },
209 [RK805_IRQ_RTC_ALARM] = {
210 .mask = RK805_IRQ_RTC_ALARM_MSK,
211 .reg_offset = 0,
212 },
213 [RK805_IRQ_RTC_PERIOD] = {
214 .mask = RK805_IRQ_RTC_PERIOD_MSK,
215 .reg_offset = 0,
216 },
217 [RK805_IRQ_PWRON_FALL] = {
218 .mask = RK805_IRQ_PWRON_FALL_MSK,
219 .reg_offset = 0,
220 },
221};
222
223static const struct regmap_irq rk808_irqs[] = {
224
225 [RK808_IRQ_VOUT_LO] = {
226 .mask = RK808_IRQ_VOUT_LO_MSK,
227 .reg_offset = 0,
228 },
229 [RK808_IRQ_VB_LO] = {
230 .mask = RK808_IRQ_VB_LO_MSK,
231 .reg_offset = 0,
232 },
233 [RK808_IRQ_PWRON] = {
234 .mask = RK808_IRQ_PWRON_MSK,
235 .reg_offset = 0,
236 },
237 [RK808_IRQ_PWRON_LP] = {
238 .mask = RK808_IRQ_PWRON_LP_MSK,
239 .reg_offset = 0,
240 },
241 [RK808_IRQ_HOTDIE] = {
242 .mask = RK808_IRQ_HOTDIE_MSK,
243 .reg_offset = 0,
244 },
245 [RK808_IRQ_RTC_ALARM] = {
246 .mask = RK808_IRQ_RTC_ALARM_MSK,
247 .reg_offset = 0,
248 },
249 [RK808_IRQ_RTC_PERIOD] = {
250 .mask = RK808_IRQ_RTC_PERIOD_MSK,
251 .reg_offset = 0,
252 },
253
254
255 [RK808_IRQ_PLUG_IN_INT] = {
256 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
257 .reg_offset = 1,
258 },
259 [RK808_IRQ_PLUG_OUT_INT] = {
260 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
261 .reg_offset = 1,
262 },
263};
264
265static const struct regmap_irq rk818_irqs[] = {
266
267 [RK818_IRQ_VOUT_LO] = {
268 .mask = RK818_IRQ_VOUT_LO_MSK,
269 .reg_offset = 0,
270 },
271 [RK818_IRQ_VB_LO] = {
272 .mask = RK818_IRQ_VB_LO_MSK,
273 .reg_offset = 0,
274 },
275 [RK818_IRQ_PWRON] = {
276 .mask = RK818_IRQ_PWRON_MSK,
277 .reg_offset = 0,
278 },
279 [RK818_IRQ_PWRON_LP] = {
280 .mask = RK818_IRQ_PWRON_LP_MSK,
281 .reg_offset = 0,
282 },
283 [RK818_IRQ_HOTDIE] = {
284 .mask = RK818_IRQ_HOTDIE_MSK,
285 .reg_offset = 0,
286 },
287 [RK818_IRQ_RTC_ALARM] = {
288 .mask = RK818_IRQ_RTC_ALARM_MSK,
289 .reg_offset = 0,
290 },
291 [RK818_IRQ_RTC_PERIOD] = {
292 .mask = RK818_IRQ_RTC_PERIOD_MSK,
293 .reg_offset = 0,
294 },
295 [RK818_IRQ_USB_OV] = {
296 .mask = RK818_IRQ_USB_OV_MSK,
297 .reg_offset = 0,
298 },
299
300
301 [RK818_IRQ_PLUG_IN] = {
302 .mask = RK818_IRQ_PLUG_IN_MSK,
303 .reg_offset = 1,
304 },
305 [RK818_IRQ_PLUG_OUT] = {
306 .mask = RK818_IRQ_PLUG_OUT_MSK,
307 .reg_offset = 1,
308 },
309 [RK818_IRQ_CHG_OK] = {
310 .mask = RK818_IRQ_CHG_OK_MSK,
311 .reg_offset = 1,
312 },
313 [RK818_IRQ_CHG_TE] = {
314 .mask = RK818_IRQ_CHG_TE_MSK,
315 .reg_offset = 1,
316 },
317 [RK818_IRQ_CHG_TS1] = {
318 .mask = RK818_IRQ_CHG_TS1_MSK,
319 .reg_offset = 1,
320 },
321 [RK818_IRQ_TS2] = {
322 .mask = RK818_IRQ_TS2_MSK,
323 .reg_offset = 1,
324 },
325 [RK818_IRQ_CHG_CVTLIM] = {
326 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
327 .reg_offset = 1,
328 },
329 [RK818_IRQ_DISCHG_ILIM] = {
330 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
331 .reg_offset = 1,
332 },
333};
334
335static 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,
344};
345
346static 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,
356};
357
358static 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,
368};
369
370static struct i2c_client *rk808_i2c_client;
371
372static void rk805_device_shutdown(void)
373{
374 int ret;
375 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
376
377 if (!rk808) {
378 dev_warn(&rk808_i2c_client->dev,
379 "have no rk805, so do nothing here\n");
380 return;
381 }
382
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");
388}
389
390static void rk808_device_shutdown(void)
391{
392 int ret;
393 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
394
395 if (!rk808) {
396 dev_warn(&rk808_i2c_client->dev,
397 "have no rk808, so do nothing here\n");
398 return;
399 }
400
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");
406}
407
408static void rk818_device_shutdown(void)
409{
410 int ret;
411 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
412
413 if (!rk808) {
414 dev_warn(&rk808_i2c_client->dev,
415 "have no rk818, so do nothing here\n");
416 return;
417 }
418
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");
424}
425
426static const struct of_device_id rk808_of_match[] = {
427 { .compatible = "rockchip,rk805" },
428 { .compatible = "rockchip,rk808" },
429 { .compatible = "rockchip,rk818" },
430 { },
431};
432MODULE_DEVICE_TABLE(of, rk808_of_match);
433
434static int rk808_probe(struct i2c_client *client,
435 const struct i2c_device_id *id)
436{
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;
447
448 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
449 if (!rk808)
450 return -ENOMEM;
451
452
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;
458 }
459
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;
465 }
466
467 rk808->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
468 dev_info(&client->dev, "chip id: 0x%x\n", (unsigned int)rk808->variant);
469
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;
502 }
503
504 rk808->i2c = client;
505 i2c_set_clientdata(client, rk808);
506
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);
511 }
512
513 if (!client->irq) {
514 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
515 return -EINVAL;
516 }
517
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;
524 }
525
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;
536 }
537 }
538
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;
545 }
546
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;
552 }
553
554 return 0;
555
556err_irq:
557 regmap_del_irq_chip(client->irq, rk808->irq_data);
558 return ret;
559}
560
561static int rk808_remove(struct i2c_client *client)
562{
563 struct rk808 *rk808 = i2c_get_clientdata(client);
564
565 regmap_del_irq_chip(client->irq, rk808->irq_data);
566 pm_power_off = NULL;
567
568 return 0;
569}
570
571static const struct i2c_device_id rk808_ids[] = {
572 { "rk805" },
573 { "rk808" },
574 { "rk818" },
575 { },
576};
577MODULE_DEVICE_TABLE(i2c, rk808_ids);
578
579static struct i2c_driver rk808_i2c_driver = {
580 .driver = {
581 .name = "rk808",
582 .of_match_table = rk808_of_match,
583 },
584 .probe = rk808_probe,
585 .remove = rk808_remove,
586 .id_table = rk808_ids,
587};
588
589module_i2c_driver(rk808_i2c_driver);
590
591MODULE_LICENSE("GPL");
592MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
593MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
594MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
595MODULE_DESCRIPTION("RK808/RK818 PMIC driver");
596