blob: 601cefb5c9d8eb2b24e6f2c194cf446a3477b680 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Chris Zhongf69a7cf2014-09-03 21:51:44 +08002/*
Wadim Egorov2eedcbf2016-08-29 13:07:58 +02003 * MFD core driver for Rockchip RK808/RK818
Chris Zhongf69a7cf2014-09-03 21:51:44 +08004 *
5 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 *
7 * Author: Chris Zhong <zyw@rock-chips.com>
8 * Author: Zhang Qing <zhangqing@rock-chips.com>
9 *
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020010 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11 *
12 * Author: Wadim Egorov <w.egorov@phytec.de>
Chris Zhongf69a7cf2014-09-03 21:51:44 +080013 */
14
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/mfd/rk808.h>
18#include <linux/mfd/core.h>
19#include <linux/module.h>
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020020#include <linux/of_device.h>
Chris Zhongf69a7cf2014-09-03 21:51:44 +080021#include <linux/regmap.h>
Tony Xie586c1b42019-06-21 06:32:54 -040022#include <linux/syscore_ops.h>
Chris Zhongf69a7cf2014-09-03 21:51:44 +080023
24struct rk808_reg_data {
25 int addr;
26 int mask;
27 int value;
28};
29
Doug Anderson2adb3b82014-09-09 16:06:04 -070030static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
31{
32 /*
33 * Notes:
34 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
35 * we don't use that feature. It's better to cache.
36 * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
37 * bits are cleared in case when we shutoff anyway, but better safe.
38 */
39
40 switch (reg) {
41 case RK808_SECONDS_REG ... RK808_WEEKS_REG:
42 case RK808_RTC_STATUS_REG:
43 case RK808_VB_MON_REG:
44 case RK808_THERMAL_REG:
45 case RK808_DCDC_UV_STS_REG:
46 case RK808_LDO_UV_STS_REG:
47 case RK808_DCDC_PG_REG:
48 case RK808_LDO_PG_REG:
49 case RK808_DEVCTRL_REG:
50 case RK808_INT_STS_REG1:
51 case RK808_INT_STS_REG2:
52 return true;
53 }
54
55 return false;
56}
57
Tony Xie586c1b42019-06-21 06:32:54 -040058static bool rk817_is_volatile_reg(struct device *dev, unsigned int reg)
59{
60 /*
61 * Notes:
62 * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
63 * we don't use that feature. It's better to cache.
64 */
65
66 switch (reg) {
67 case RK817_SECONDS_REG ... RK817_WEEKS_REG:
68 case RK817_RTC_STATUS_REG:
69 case RK817_INT_STS_REG0:
70 case RK817_INT_STS_REG1:
71 case RK817_INT_STS_REG2:
72 case RK817_SYS_STS:
73 return true;
74 }
75
76 return true;
77}
78
Wadim Egorov2eedcbf2016-08-29 13:07:58 +020079static const struct regmap_config rk818_regmap_config = {
80 .reg_bits = 8,
81 .val_bits = 8,
82 .max_register = RK818_USB_CTRL_REG,
83 .cache_type = REGCACHE_RBTREE,
84 .volatile_reg = rk808_is_volatile_reg,
85};
86
Elaine Zhang990f05f2017-08-21 03:28:38 +020087static const struct regmap_config rk805_regmap_config = {
88 .reg_bits = 8,
89 .val_bits = 8,
90 .max_register = RK805_OFF_SOURCE_REG,
91 .cache_type = REGCACHE_RBTREE,
92 .volatile_reg = rk808_is_volatile_reg,
93};
94
Chris Zhongf69a7cf2014-09-03 21:51:44 +080095static const struct regmap_config rk808_regmap_config = {
96 .reg_bits = 8,
97 .val_bits = 8,
98 .max_register = RK808_IO_POL_REG,
Doug Anderson2adb3b82014-09-09 16:06:04 -070099 .cache_type = REGCACHE_RBTREE,
100 .volatile_reg = rk808_is_volatile_reg,
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800101};
102
Tony Xie586c1b42019-06-21 06:32:54 -0400103static const struct regmap_config rk817_regmap_config = {
104 .reg_bits = 8,
105 .val_bits = 8,
106 .max_register = RK817_GPIO_INT_CFG,
107 .cache_type = REGCACHE_NONE,
108 .volatile_reg = rk817_is_volatile_reg,
109};
110
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800111static struct resource rtc_resources[] = {
112 {
113 .start = RK808_IRQ_RTC_ALARM,
114 .end = RK808_IRQ_RTC_ALARM,
115 .flags = IORESOURCE_IRQ,
116 }
117};
118
Tony Xie586c1b42019-06-21 06:32:54 -0400119static struct resource rk817_rtc_resources[] = {
120 DEFINE_RES_IRQ(RK817_IRQ_RTC_ALARM),
121};
122
Joseph Chenf7c22392017-08-21 03:28:42 +0200123static struct resource rk805_key_resources[] = {
124 {
125 .start = RK805_IRQ_PWRON_FALL,
126 .end = RK805_IRQ_PWRON_FALL,
127 .flags = IORESOURCE_IRQ,
128 },
129 {
130 .start = RK805_IRQ_PWRON_RISE,
131 .end = RK805_IRQ_PWRON_RISE,
132 .flags = IORESOURCE_IRQ,
133 }
134};
135
Tony Xie586c1b42019-06-21 06:32:54 -0400136static struct resource rk817_pwrkey_resources[] = {
137 DEFINE_RES_IRQ(RK817_IRQ_PWRON_RISE),
138 DEFINE_RES_IRQ(RK817_IRQ_PWRON_FALL),
139};
140
Elaine Zhang990f05f2017-08-21 03:28:38 +0200141static const struct mfd_cell rk805s[] = {
142 { .name = "rk808-clkout", },
143 { .name = "rk808-regulator", },
Joseph Chen8d249b62017-08-21 03:28:41 +0200144 { .name = "rk805-pinctrl", },
Elaine Zhang990f05f2017-08-21 03:28:38 +0200145 {
146 .name = "rk808-rtc",
147 .num_resources = ARRAY_SIZE(rtc_resources),
148 .resources = &rtc_resources[0],
149 },
Joseph Chenf7c22392017-08-21 03:28:42 +0200150 { .name = "rk805-pwrkey",
151 .num_resources = ARRAY_SIZE(rk805_key_resources),
152 .resources = &rk805_key_resources[0],
153 },
Elaine Zhang990f05f2017-08-21 03:28:38 +0200154};
155
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800156static const struct mfd_cell rk808s[] = {
157 { .name = "rk808-clkout", },
158 { .name = "rk808-regulator", },
159 {
160 .name = "rk808-rtc",
161 .num_resources = ARRAY_SIZE(rtc_resources),
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200162 .resources = rtc_resources,
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800163 },
164};
165
Tony Xie586c1b42019-06-21 06:32:54 -0400166static const struct mfd_cell rk817s[] = {
167 { .name = "rk808-clkout",},
168 { .name = "rk808-regulator",},
169 {
170 .name = "rk8xx-pwrkey",
171 .num_resources = ARRAY_SIZE(rk817_pwrkey_resources),
172 .resources = &rk817_pwrkey_resources[0],
173 },
174 {
175 .name = "rk808-rtc",
176 .num_resources = ARRAY_SIZE(rk817_rtc_resources),
177 .resources = &rk817_rtc_resources[0],
178 },
179};
180
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200181static const struct mfd_cell rk818s[] = {
182 { .name = "rk808-clkout", },
183 { .name = "rk808-regulator", },
184 {
185 .name = "rk808-rtc",
186 .num_resources = ARRAY_SIZE(rtc_resources),
187 .resources = rtc_resources,
188 },
189};
190
Elaine Zhang990f05f2017-08-21 03:28:38 +0200191static const struct rk808_reg_data rk805_pre_init_reg[] = {
192 {RK805_BUCK1_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
193 RK805_BUCK1_2_ILMAX_4000MA},
194 {RK805_BUCK2_CONFIG_REG, RK805_BUCK1_2_ILMAX_MASK,
195 RK805_BUCK1_2_ILMAX_4000MA},
196 {RK805_BUCK3_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
197 RK805_BUCK3_ILMAX_3000MA},
198 {RK805_BUCK4_CONFIG_REG, RK805_BUCK3_4_ILMAX_MASK,
199 RK805_BUCK4_ILMAX_3500MA},
200 {RK805_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_400MA},
201 {RK805_GPIO_IO_POL_REG, SLP_SD_MSK, SLEEP_FUN},
202 {RK805_THERMAL_REG, TEMP_HOTDIE_MSK, TEMP115C},
203};
204
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200205static const struct rk808_reg_data rk808_pre_init_reg[] = {
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800206 { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
207 { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
208 { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
209 { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
210 { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
Chris Zhonge19f7422015-02-28 18:09:06 +0800211 { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800212 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
213 VB_LO_SEL_3500MV },
214};
215
Tony Xie586c1b42019-06-21 06:32:54 -0400216static const struct rk808_reg_data rk817_pre_init_reg[] = {
217 {RK817_RTC_CTRL_REG, RTC_STOP, RTC_STOP},
218 {RK817_GPIO_INT_CFG, RK817_INT_POL_MSK, RK817_INT_POL_H},
219 {RK817_SYS_CFG(1), RK817_HOTDIE_TEMP_MSK | RK817_TSD_TEMP_MSK,
220 RK817_HOTDIE_105 | RK817_TSD_140},
221};
222
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200223static const struct rk808_reg_data rk818_pre_init_reg[] = {
224 /* improve efficiency */
225 { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
226 { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
227 { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
228 { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
229 RK818_USB_ILMIN_2000MA },
230 /* close charger when usb lower then 3.4V */
231 { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
232 (0x7 << 4) },
233 /* no action when vref */
234 { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
235 /* enable HDMI 5V */
236 { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
237 { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
238 VB_LO_SEL_3500MV },
239};
240
Elaine Zhang990f05f2017-08-21 03:28:38 +0200241static const struct regmap_irq rk805_irqs[] = {
242 [RK805_IRQ_PWRON_RISE] = {
243 .mask = RK805_IRQ_PWRON_RISE_MSK,
244 .reg_offset = 0,
245 },
246 [RK805_IRQ_VB_LOW] = {
247 .mask = RK805_IRQ_VB_LOW_MSK,
248 .reg_offset = 0,
249 },
250 [RK805_IRQ_PWRON] = {
251 .mask = RK805_IRQ_PWRON_MSK,
252 .reg_offset = 0,
253 },
254 [RK805_IRQ_PWRON_LP] = {
255 .mask = RK805_IRQ_PWRON_LP_MSK,
256 .reg_offset = 0,
257 },
258 [RK805_IRQ_HOTDIE] = {
259 .mask = RK805_IRQ_HOTDIE_MSK,
260 .reg_offset = 0,
261 },
262 [RK805_IRQ_RTC_ALARM] = {
263 .mask = RK805_IRQ_RTC_ALARM_MSK,
264 .reg_offset = 0,
265 },
266 [RK805_IRQ_RTC_PERIOD] = {
267 .mask = RK805_IRQ_RTC_PERIOD_MSK,
268 .reg_offset = 0,
269 },
270 [RK805_IRQ_PWRON_FALL] = {
271 .mask = RK805_IRQ_PWRON_FALL_MSK,
272 .reg_offset = 0,
273 },
274};
275
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800276static const struct regmap_irq rk808_irqs[] = {
277 /* INT_STS */
278 [RK808_IRQ_VOUT_LO] = {
279 .mask = RK808_IRQ_VOUT_LO_MSK,
280 .reg_offset = 0,
281 },
282 [RK808_IRQ_VB_LO] = {
283 .mask = RK808_IRQ_VB_LO_MSK,
284 .reg_offset = 0,
285 },
286 [RK808_IRQ_PWRON] = {
287 .mask = RK808_IRQ_PWRON_MSK,
288 .reg_offset = 0,
289 },
290 [RK808_IRQ_PWRON_LP] = {
291 .mask = RK808_IRQ_PWRON_LP_MSK,
292 .reg_offset = 0,
293 },
294 [RK808_IRQ_HOTDIE] = {
295 .mask = RK808_IRQ_HOTDIE_MSK,
296 .reg_offset = 0,
297 },
298 [RK808_IRQ_RTC_ALARM] = {
299 .mask = RK808_IRQ_RTC_ALARM_MSK,
300 .reg_offset = 0,
301 },
302 [RK808_IRQ_RTC_PERIOD] = {
303 .mask = RK808_IRQ_RTC_PERIOD_MSK,
304 .reg_offset = 0,
305 },
306
307 /* INT_STS2 */
308 [RK808_IRQ_PLUG_IN_INT] = {
309 .mask = RK808_IRQ_PLUG_IN_INT_MSK,
310 .reg_offset = 1,
311 },
312 [RK808_IRQ_PLUG_OUT_INT] = {
313 .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
314 .reg_offset = 1,
315 },
316};
317
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200318static const struct regmap_irq rk818_irqs[] = {
319 /* INT_STS */
320 [RK818_IRQ_VOUT_LO] = {
321 .mask = RK818_IRQ_VOUT_LO_MSK,
322 .reg_offset = 0,
323 },
324 [RK818_IRQ_VB_LO] = {
325 .mask = RK818_IRQ_VB_LO_MSK,
326 .reg_offset = 0,
327 },
328 [RK818_IRQ_PWRON] = {
329 .mask = RK818_IRQ_PWRON_MSK,
330 .reg_offset = 0,
331 },
332 [RK818_IRQ_PWRON_LP] = {
333 .mask = RK818_IRQ_PWRON_LP_MSK,
334 .reg_offset = 0,
335 },
336 [RK818_IRQ_HOTDIE] = {
337 .mask = RK818_IRQ_HOTDIE_MSK,
338 .reg_offset = 0,
339 },
340 [RK818_IRQ_RTC_ALARM] = {
341 .mask = RK818_IRQ_RTC_ALARM_MSK,
342 .reg_offset = 0,
343 },
344 [RK818_IRQ_RTC_PERIOD] = {
345 .mask = RK818_IRQ_RTC_PERIOD_MSK,
346 .reg_offset = 0,
347 },
348 [RK818_IRQ_USB_OV] = {
349 .mask = RK818_IRQ_USB_OV_MSK,
350 .reg_offset = 0,
351 },
352
353 /* INT_STS2 */
354 [RK818_IRQ_PLUG_IN] = {
355 .mask = RK818_IRQ_PLUG_IN_MSK,
356 .reg_offset = 1,
357 },
358 [RK818_IRQ_PLUG_OUT] = {
359 .mask = RK818_IRQ_PLUG_OUT_MSK,
360 .reg_offset = 1,
361 },
362 [RK818_IRQ_CHG_OK] = {
363 .mask = RK818_IRQ_CHG_OK_MSK,
364 .reg_offset = 1,
365 },
366 [RK818_IRQ_CHG_TE] = {
367 .mask = RK818_IRQ_CHG_TE_MSK,
368 .reg_offset = 1,
369 },
370 [RK818_IRQ_CHG_TS1] = {
371 .mask = RK818_IRQ_CHG_TS1_MSK,
372 .reg_offset = 1,
373 },
374 [RK818_IRQ_TS2] = {
375 .mask = RK818_IRQ_TS2_MSK,
376 .reg_offset = 1,
377 },
378 [RK818_IRQ_CHG_CVTLIM] = {
379 .mask = RK818_IRQ_CHG_CVTLIM_MSK,
380 .reg_offset = 1,
381 },
382 [RK818_IRQ_DISCHG_ILIM] = {
383 .mask = RK818_IRQ_DISCHG_ILIM_MSK,
384 .reg_offset = 1,
385 },
386};
387
Tony Xie586c1b42019-06-21 06:32:54 -0400388static const struct regmap_irq rk817_irqs[RK817_IRQ_END] = {
389 REGMAP_IRQ_REG_LINE(0, 8),
390 REGMAP_IRQ_REG_LINE(1, 8),
391 REGMAP_IRQ_REG_LINE(2, 8),
392 REGMAP_IRQ_REG_LINE(3, 8),
393 REGMAP_IRQ_REG_LINE(4, 8),
394 REGMAP_IRQ_REG_LINE(5, 8),
395 REGMAP_IRQ_REG_LINE(6, 8),
396 REGMAP_IRQ_REG_LINE(7, 8),
397 REGMAP_IRQ_REG_LINE(8, 8),
398 REGMAP_IRQ_REG_LINE(9, 8),
399 REGMAP_IRQ_REG_LINE(10, 8),
400 REGMAP_IRQ_REG_LINE(11, 8),
401 REGMAP_IRQ_REG_LINE(12, 8),
402 REGMAP_IRQ_REG_LINE(13, 8),
403 REGMAP_IRQ_REG_LINE(14, 8),
404 REGMAP_IRQ_REG_LINE(15, 8),
405 REGMAP_IRQ_REG_LINE(16, 8),
406 REGMAP_IRQ_REG_LINE(17, 8),
407 REGMAP_IRQ_REG_LINE(18, 8),
408 REGMAP_IRQ_REG_LINE(19, 8),
409 REGMAP_IRQ_REG_LINE(20, 8),
410 REGMAP_IRQ_REG_LINE(21, 8),
411 REGMAP_IRQ_REG_LINE(22, 8),
412 REGMAP_IRQ_REG_LINE(23, 8)
413};
414
Elaine Zhang990f05f2017-08-21 03:28:38 +0200415static struct regmap_irq_chip rk805_irq_chip = {
416 .name = "rk805",
417 .irqs = rk805_irqs,
418 .num_irqs = ARRAY_SIZE(rk805_irqs),
419 .num_regs = 1,
420 .status_base = RK805_INT_STS_REG,
421 .mask_base = RK805_INT_STS_MSK_REG,
422 .ack_base = RK805_INT_STS_REG,
423 .init_ack_masked = true,
424};
425
Bhumika Goyal24e34b92017-01-25 00:55:24 +0530426static const struct regmap_irq_chip rk808_irq_chip = {
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800427 .name = "rk808",
428 .irqs = rk808_irqs,
429 .num_irqs = ARRAY_SIZE(rk808_irqs),
430 .num_regs = 2,
431 .irq_reg_stride = 2,
432 .status_base = RK808_INT_STS_REG1,
433 .mask_base = RK808_INT_STS_MSK_REG1,
434 .ack_base = RK808_INT_STS_REG1,
435 .init_ack_masked = true,
436};
437
Tony Xie586c1b42019-06-21 06:32:54 -0400438static struct regmap_irq_chip rk817_irq_chip = {
439 .name = "rk817",
440 .irqs = rk817_irqs,
441 .num_irqs = ARRAY_SIZE(rk817_irqs),
442 .num_regs = 3,
443 .irq_reg_stride = 2,
444 .status_base = RK817_INT_STS_REG0,
445 .mask_base = RK817_INT_STS_MSK_REG0,
446 .ack_base = RK817_INT_STS_REG0,
447 .init_ack_masked = true,
448};
449
Bhumika Goyal24e34b92017-01-25 00:55:24 +0530450static const struct regmap_irq_chip rk818_irq_chip = {
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200451 .name = "rk818",
452 .irqs = rk818_irqs,
453 .num_irqs = ARRAY_SIZE(rk818_irqs),
454 .num_regs = 2,
455 .irq_reg_stride = 2,
456 .status_base = RK818_INT_STS_REG1,
457 .mask_base = RK818_INT_STS_MSK_REG1,
458 .ack_base = RK818_INT_STS_REG1,
459 .init_ack_masked = true,
460};
461
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800462static struct i2c_client *rk808_i2c_client;
Elaine Zhang990f05f2017-08-21 03:28:38 +0200463
464static void rk805_device_shutdown(void)
465{
466 int ret;
467 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
468
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300469 if (!rk808)
Elaine Zhang990f05f2017-08-21 03:28:38 +0200470 return;
Elaine Zhang990f05f2017-08-21 03:28:38 +0200471
472 ret = regmap_update_bits(rk808->regmap,
473 RK805_DEV_CTRL_REG,
474 DEV_OFF, DEV_OFF);
475 if (ret)
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300476 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
477}
478
479static void rk805_device_shutdown_prepare(void)
480{
481 int ret;
482 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
483
484 if (!rk808)
485 return;
486
487 ret = regmap_update_bits(rk808->regmap,
488 RK805_GPIO_IO_POL_REG,
489 SLP_SD_MSK, SHUTDOWN_FUN);
490 if (ret)
491 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
Elaine Zhang990f05f2017-08-21 03:28:38 +0200492}
493
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800494static void rk808_device_shutdown(void)
495{
496 int ret;
497 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
498
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300499 if (!rk808)
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800500 return;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800501
502 ret = regmap_update_bits(rk808->regmap,
503 RK808_DEVCTRL_REG,
504 DEV_OFF_RST, DEV_OFF_RST);
505 if (ret)
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300506 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800507}
508
Jianhong Chenb2e2c852016-10-17 17:03:10 +0800509static void rk818_device_shutdown(void)
510{
511 int ret;
512 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
513
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300514 if (!rk808)
Jianhong Chenb2e2c852016-10-17 17:03:10 +0800515 return;
Jianhong Chenb2e2c852016-10-17 17:03:10 +0800516
517 ret = regmap_update_bits(rk808->regmap,
518 RK818_DEVCTRL_REG,
519 DEV_OFF, DEV_OFF);
520 if (ret)
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300521 dev_err(&rk808_i2c_client->dev, "Failed to shutdown device!\n");
Jianhong Chenb2e2c852016-10-17 17:03:10 +0800522}
523
Tony Xie586c1b42019-06-21 06:32:54 -0400524static void rk8xx_syscore_shutdown(void)
525{
526 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
527 int ret;
528
529 if (system_state == SYSTEM_POWER_OFF &&
530 (rk808->variant == RK809_ID || rk808->variant == RK817_ID)) {
531 ret = regmap_update_bits(rk808->regmap,
532 RK817_SYS_CFG(3),
533 RK817_SLPPIN_FUNC_MSK,
534 SLPPIN_DN_FUN);
535 if (ret) {
536 dev_warn(&rk808_i2c_client->dev,
537 "Cannot switch to power down function\n");
538 }
539 }
540}
541
542static struct syscore_ops rk808_syscore_ops = {
543 .shutdown = rk8xx_syscore_shutdown,
544};
545
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200546static const struct of_device_id rk808_of_match[] = {
Elaine Zhang990f05f2017-08-21 03:28:38 +0200547 { .compatible = "rockchip,rk805" },
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200548 { .compatible = "rockchip,rk808" },
Tony Xie586c1b42019-06-21 06:32:54 -0400549 { .compatible = "rockchip,rk809" },
550 { .compatible = "rockchip,rk817" },
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200551 { .compatible = "rockchip,rk818" },
552 { },
553};
554MODULE_DEVICE_TABLE(of, rk808_of_match);
555
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800556static int rk808_probe(struct i2c_client *client,
557 const struct i2c_device_id *id)
558{
559 struct device_node *np = client->dev.of_node;
560 struct rk808 *rk808;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200561 const struct rk808_reg_data *pre_init_reg;
562 const struct mfd_cell *cells;
563 int nr_pre_init_regs;
564 int nr_cells;
Elaine Zhang9d6105e2017-08-21 03:28:34 +0200565 int pm_off = 0, msb, lsb;
Tony Xie586c1b42019-06-21 06:32:54 -0400566 unsigned char pmic_id_msb, pmic_id_lsb;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800567 int ret;
568 int i;
569
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200570 rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
571 if (!rk808)
572 return -ENOMEM;
573
Tony Xie586c1b42019-06-21 06:32:54 -0400574 if (of_device_is_compatible(np, "rockchip,rk817") ||
575 of_device_is_compatible(np, "rockchip,rk809")) {
576 pmic_id_msb = RK817_ID_MSB;
577 pmic_id_lsb = RK817_ID_LSB;
578 } else {
579 pmic_id_msb = RK808_ID_MSB;
580 pmic_id_lsb = RK808_ID_LSB;
581 }
582
Elaine Zhang9d6105e2017-08-21 03:28:34 +0200583 /* Read chip variant */
Tony Xie586c1b42019-06-21 06:32:54 -0400584 msb = i2c_smbus_read_byte_data(client, pmic_id_msb);
Elaine Zhang9d6105e2017-08-21 03:28:34 +0200585 if (msb < 0) {
586 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200587 RK808_ID_MSB);
Elaine Zhang9d6105e2017-08-21 03:28:34 +0200588 return msb;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200589 }
590
Tony Xie586c1b42019-06-21 06:32:54 -0400591 lsb = i2c_smbus_read_byte_data(client, pmic_id_lsb);
Elaine Zhang9d6105e2017-08-21 03:28:34 +0200592 if (lsb < 0) {
593 dev_err(&client->dev, "failed to read the chip id at 0x%x\n",
594 RK808_ID_LSB);
595 return lsb;
596 }
597
598 rk808->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
599 dev_info(&client->dev, "chip id: 0x%x\n", (unsigned int)rk808->variant);
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200600
601 switch (rk808->variant) {
Elaine Zhang990f05f2017-08-21 03:28:38 +0200602 case RK805_ID:
603 rk808->regmap_cfg = &rk805_regmap_config;
604 rk808->regmap_irq_chip = &rk805_irq_chip;
605 pre_init_reg = rk805_pre_init_reg;
606 nr_pre_init_regs = ARRAY_SIZE(rk805_pre_init_reg);
607 cells = rk805s;
608 nr_cells = ARRAY_SIZE(rk805s);
Stefan Mavrodiev76304992019-06-07 15:42:25 +0300609 rk808->pm_pwroff_fn = rk805_device_shutdown;
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300610 rk808->pm_pwroff_prep_fn = rk805_device_shutdown_prepare;
Elaine Zhang990f05f2017-08-21 03:28:38 +0200611 break;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200612 case RK808_ID:
613 rk808->regmap_cfg = &rk808_regmap_config;
614 rk808->regmap_irq_chip = &rk808_irq_chip;
615 pre_init_reg = rk808_pre_init_reg;
616 nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
617 cells = rk808s;
618 nr_cells = ARRAY_SIZE(rk808s);
Stefan Mavrodiev76304992019-06-07 15:42:25 +0300619 rk808->pm_pwroff_fn = rk808_device_shutdown;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200620 break;
621 case RK818_ID:
622 rk808->regmap_cfg = &rk818_regmap_config;
623 rk808->regmap_irq_chip = &rk818_irq_chip;
624 pre_init_reg = rk818_pre_init_reg;
625 nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
626 cells = rk818s;
627 nr_cells = ARRAY_SIZE(rk818s);
Stefan Mavrodiev76304992019-06-07 15:42:25 +0300628 rk808->pm_pwroff_fn = rk818_device_shutdown;
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200629 break;
Tony Xie586c1b42019-06-21 06:32:54 -0400630 case RK809_ID:
631 case RK817_ID:
632 rk808->regmap_cfg = &rk817_regmap_config;
633 rk808->regmap_irq_chip = &rk817_irq_chip;
634 pre_init_reg = rk817_pre_init_reg;
635 nr_pre_init_regs = ARRAY_SIZE(rk817_pre_init_reg);
636 cells = rk817s;
637 nr_cells = ARRAY_SIZE(rk817s);
638 register_syscore_ops(&rk808_syscore_ops);
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200639 break;
640 default:
641 dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
642 rk808->variant);
643 return -EINVAL;
644 }
645
646 rk808->i2c = client;
647 i2c_set_clientdata(client, rk808);
648
649 rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
650 if (IS_ERR(rk808->regmap)) {
651 dev_err(&client->dev, "regmap initialization failed\n");
652 return PTR_ERR(rk808->regmap);
653 }
654
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800655 if (!client->irq) {
656 dev_err(&client->dev, "No interrupt support, no core IRQ\n");
657 return -EINVAL;
658 }
659
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800660 ret = regmap_add_irq_chip(rk808->regmap, client->irq,
661 IRQF_ONESHOT, -1,
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200662 rk808->regmap_irq_chip, &rk808->irq_data);
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800663 if (ret) {
664 dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
665 return ret;
666 }
667
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200668 for (i = 0; i < nr_pre_init_regs; i++) {
669 ret = regmap_update_bits(rk808->regmap,
670 pre_init_reg[i].addr,
671 pre_init_reg[i].mask,
672 pre_init_reg[i].value);
673 if (ret) {
674 dev_err(&client->dev,
675 "0x%x write err\n",
676 pre_init_reg[i].addr);
677 return ret;
678 }
679 }
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800680
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200681 ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
682 cells, nr_cells, NULL, 0,
683 regmap_irq_get_domain(rk808->irq_data));
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800684 if (ret) {
685 dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
686 goto err_irq;
687 }
688
689 pm_off = of_property_read_bool(np,
690 "rockchip,system-power-controller");
691 if (pm_off && !pm_power_off) {
692 rk808_i2c_client = client;
Stefan Mavrodiev76304992019-06-07 15:42:25 +0300693 pm_power_off = rk808->pm_pwroff_fn;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800694 }
695
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300696 if (pm_off && !pm_power_off_prepare) {
697 if (!rk808_i2c_client)
698 rk808_i2c_client = client;
699 pm_power_off_prepare = rk808->pm_pwroff_prep_fn;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800700 }
701
702 return 0;
703
704err_irq:
705 regmap_del_irq_chip(client->irq, rk808->irq_data);
706 return ret;
707}
708
709static int rk808_remove(struct i2c_client *client)
710{
711 struct rk808 *rk808 = i2c_get_clientdata(client);
712
713 regmap_del_irq_chip(client->irq, rk808->irq_data);
Stefan Mavrodiev76304992019-06-07 15:42:25 +0300714
715 /**
716 * pm_power_off may points to a function from another module.
717 * Check if the pointer is set by us and only then overwrite it.
718 */
719 if (rk808->pm_pwroff_fn && pm_power_off == rk808->pm_pwroff_fn)
720 pm_power_off = NULL;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800721
Stefan Mavrodievac195d92019-06-07 15:42:26 +0300722 /**
723 * As above, check if the pointer is set by us before overwrite.
724 */
725 if (rk808->pm_pwroff_prep_fn &&
726 pm_power_off_prepare == rk808->pm_pwroff_prep_fn)
727 pm_power_off_prepare = NULL;
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800728
729 return 0;
730}
731
Tony Xie586c1b42019-06-21 06:32:54 -0400732static int rk8xx_suspend(struct device *dev)
733{
734 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
735 int ret = 0;
736
737 switch (rk808->variant) {
738 case RK809_ID:
739 case RK817_ID:
740 ret = regmap_update_bits(rk808->regmap,
741 RK817_SYS_CFG(3),
742 RK817_SLPPIN_FUNC_MSK,
743 SLPPIN_SLP_FUN);
744 break;
745 default:
746 break;
747 }
748
749 return ret;
750}
751
752static int rk8xx_resume(struct device *dev)
753{
754 struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
755 int ret = 0;
756
757 switch (rk808->variant) {
758 case RK809_ID:
759 case RK817_ID:
760 ret = regmap_update_bits(rk808->regmap,
761 RK817_SYS_CFG(3),
762 RK817_SLPPIN_FUNC_MSK,
763 SLPPIN_NULL_FUN);
764 break;
765 default:
766 break;
767 }
768
769 return ret;
770}
771SIMPLE_DEV_PM_OPS(rk8xx_pm_ops, rk8xx_suspend, rk8xx_resume);
772
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800773static struct i2c_driver rk808_i2c_driver = {
774 .driver = {
775 .name = "rk808",
776 .of_match_table = rk808_of_match,
Tony Xie586c1b42019-06-21 06:32:54 -0400777 .pm = &rk8xx_pm_ops,
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800778 },
779 .probe = rk808_probe,
780 .remove = rk808_remove,
Chris Zhongf69a7cf2014-09-03 21:51:44 +0800781};
782
783module_i2c_driver(rk808_i2c_driver);
784
785MODULE_LICENSE("GPL");
786MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
787MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
Wadim Egorov2eedcbf2016-08-29 13:07:58 +0200788MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
789MODULE_DESCRIPTION("RK808/RK818 PMIC driver");