1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Core driver for ams AS3722 PMICs
5 * Copyright (C) 2013 AMS AG
6 * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
8 * Author: Florian Lobmaier <florian.lobmaier@ams.com>
9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/as3722.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
24 #define AS3722_DEVICE_ID 0x0C
26 static const struct resource as3722_rtc_resource
[] = {
27 DEFINE_RES_IRQ_NAMED(AS3722_IRQ_RTC_ALARM
, "as3722-rtc-alarm"),
30 static const struct resource as3722_adc_resource
[] = {
31 DEFINE_RES_IRQ_NAMED(AS3722_IRQ_ADC
, "as3722-adc"),
34 static const struct mfd_cell as3722_devs
[] = {
36 .name
= "as3722-pinctrl",
39 .name
= "as3722-regulator",
43 .num_resources
= ARRAY_SIZE(as3722_rtc_resource
),
44 .resources
= as3722_rtc_resource
,
48 .num_resources
= ARRAY_SIZE(as3722_adc_resource
),
49 .resources
= as3722_adc_resource
,
52 .name
= "as3722-power-off",
59 static const struct regmap_irq as3722_irqs
[] = {
62 .mask
= AS3722_INTERRUPT_MASK1_LID
,
65 .mask
= AS3722_INTERRUPT_MASK1_ACOK
,
67 [AS3722_IRQ_ENABLE1
] = {
68 .mask
= AS3722_INTERRUPT_MASK1_ENABLE1
,
70 [AS3722_IRQ_OCCUR_ALARM_SD0
] = {
71 .mask
= AS3722_INTERRUPT_MASK1_OCURR_ALARM_SD0
,
73 [AS3722_IRQ_ONKEY_LONG_PRESS
] = {
74 .mask
= AS3722_INTERRUPT_MASK1_ONKEY_LONG
,
76 [AS3722_IRQ_ONKEY
] = {
77 .mask
= AS3722_INTERRUPT_MASK1_ONKEY
,
79 [AS3722_IRQ_OVTMP
] = {
80 .mask
= AS3722_INTERRUPT_MASK1_OVTMP
,
82 [AS3722_IRQ_LOWBAT
] = {
83 .mask
= AS3722_INTERRUPT_MASK1_LOWBAT
,
87 [AS3722_IRQ_SD0_LV
] = {
88 .mask
= AS3722_INTERRUPT_MASK2_SD0_LV
,
91 [AS3722_IRQ_SD1_LV
] = {
92 .mask
= AS3722_INTERRUPT_MASK2_SD1_LV
,
95 [AS3722_IRQ_SD2_LV
] = {
96 .mask
= AS3722_INTERRUPT_MASK2_SD2345_LV
,
99 [AS3722_IRQ_PWM1_OV_PROT
] = {
100 .mask
= AS3722_INTERRUPT_MASK2_PWM1_OV_PROT
,
103 [AS3722_IRQ_PWM2_OV_PROT
] = {
104 .mask
= AS3722_INTERRUPT_MASK2_PWM2_OV_PROT
,
107 [AS3722_IRQ_ENABLE2
] = {
108 .mask
= AS3722_INTERRUPT_MASK2_ENABLE2
,
111 [AS3722_IRQ_SD6_LV
] = {
112 .mask
= AS3722_INTERRUPT_MASK2_SD6_LV
,
115 [AS3722_IRQ_RTC_REP
] = {
116 .mask
= AS3722_INTERRUPT_MASK2_RTC_REP
,
121 [AS3722_IRQ_RTC_ALARM
] = {
122 .mask
= AS3722_INTERRUPT_MASK3_RTC_ALARM
,
125 [AS3722_IRQ_GPIO1
] = {
126 .mask
= AS3722_INTERRUPT_MASK3_GPIO1
,
129 [AS3722_IRQ_GPIO2
] = {
130 .mask
= AS3722_INTERRUPT_MASK3_GPIO2
,
133 [AS3722_IRQ_GPIO3
] = {
134 .mask
= AS3722_INTERRUPT_MASK3_GPIO3
,
137 [AS3722_IRQ_GPIO4
] = {
138 .mask
= AS3722_INTERRUPT_MASK3_GPIO4
,
141 [AS3722_IRQ_GPIO5
] = {
142 .mask
= AS3722_INTERRUPT_MASK3_GPIO5
,
145 [AS3722_IRQ_WATCHDOG
] = {
146 .mask
= AS3722_INTERRUPT_MASK3_WATCHDOG
,
149 [AS3722_IRQ_ENABLE3
] = {
150 .mask
= AS3722_INTERRUPT_MASK3_ENABLE3
,
155 [AS3722_IRQ_TEMP_SD0_SHUTDOWN
] = {
156 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD0_SHUTDOWN
,
159 [AS3722_IRQ_TEMP_SD1_SHUTDOWN
] = {
160 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD1_SHUTDOWN
,
163 [AS3722_IRQ_TEMP_SD2_SHUTDOWN
] = {
164 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD6_SHUTDOWN
,
167 [AS3722_IRQ_TEMP_SD0_ALARM
] = {
168 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD0_ALARM
,
171 [AS3722_IRQ_TEMP_SD1_ALARM
] = {
172 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD1_ALARM
,
175 [AS3722_IRQ_TEMP_SD6_ALARM
] = {
176 .mask
= AS3722_INTERRUPT_MASK4_TEMP_SD6_ALARM
,
179 [AS3722_IRQ_OCCUR_ALARM_SD6
] = {
180 .mask
= AS3722_INTERRUPT_MASK4_OCCUR_ALARM_SD6
,
184 .mask
= AS3722_INTERRUPT_MASK4_ADC
,
189 static const struct regmap_irq_chip as3722_irq_chip
= {
192 .num_irqs
= ARRAY_SIZE(as3722_irqs
),
194 .status_base
= AS3722_INTERRUPT_STATUS1_REG
,
195 .mask_base
= AS3722_INTERRUPT_MASK1_REG
,
198 static int as3722_check_device_id(struct as3722
*as3722
)
203 /* Check that this is actually a AS3722 */
204 ret
= as3722_read(as3722
, AS3722_ASIC_ID1_REG
, &val
);
206 dev_err(as3722
->dev
, "ASIC_ID1 read failed: %d\n", ret
);
210 if (val
!= AS3722_DEVICE_ID
) {
211 dev_err(as3722
->dev
, "Device is not AS3722, ID is 0x%x\n", val
);
215 ret
= as3722_read(as3722
, AS3722_ASIC_ID2_REG
, &val
);
217 dev_err(as3722
->dev
, "ASIC_ID2 read failed: %d\n", ret
);
221 dev_info(as3722
->dev
, "AS3722 with revision 0x%x found\n", val
);
225 static int as3722_configure_pullups(struct as3722
*as3722
)
230 if (as3722
->en_intern_int_pullup
)
231 val
|= AS3722_INT_PULL_UP
;
232 if (as3722
->en_intern_i2c_pullup
)
233 val
|= AS3722_I2C_PULL_UP
;
235 ret
= as3722_update_bits(as3722
, AS3722_IOVOLTAGE_REG
,
236 AS3722_INT_PULL_UP
| AS3722_I2C_PULL_UP
, val
);
238 dev_err(as3722
->dev
, "IOVOLTAGE_REG update failed: %d\n", ret
);
242 static const struct regmap_range as3722_readable_ranges
[] = {
243 regmap_reg_range(AS3722_SD0_VOLTAGE_REG
, AS3722_SD6_VOLTAGE_REG
),
244 regmap_reg_range(AS3722_GPIO0_CONTROL_REG
, AS3722_LDO7_VOLTAGE_REG
),
245 regmap_reg_range(AS3722_LDO9_VOLTAGE_REG
, AS3722_REG_SEQU_MOD3_REG
),
246 regmap_reg_range(AS3722_SD_PHSW_CTRL_REG
, AS3722_PWM_CONTROL_H_REG
),
247 regmap_reg_range(AS3722_WATCHDOG_TIMER_REG
, AS3722_WATCHDOG_TIMER_REG
),
248 regmap_reg_range(AS3722_WATCHDOG_SOFTWARE_SIGNAL_REG
,
249 AS3722_BATTERY_VOLTAGE_MONITOR2_REG
),
250 regmap_reg_range(AS3722_SD_CONTROL_REG
, AS3722_PWM_VCONTROL4_REG
),
251 regmap_reg_range(AS3722_BB_CHARGER_REG
, AS3722_SRAM_REG
),
252 regmap_reg_range(AS3722_RTC_ACCESS_REG
, AS3722_RTC_ACCESS_REG
),
253 regmap_reg_range(AS3722_RTC_STATUS_REG
, AS3722_TEMP_STATUS_REG
),
254 regmap_reg_range(AS3722_ADC0_CONTROL_REG
, AS3722_ADC_CONFIGURATION_REG
),
255 regmap_reg_range(AS3722_ASIC_ID1_REG
, AS3722_ASIC_ID2_REG
),
256 regmap_reg_range(AS3722_LOCK_REG
, AS3722_LOCK_REG
),
257 regmap_reg_range(AS3722_FUSE7_REG
, AS3722_FUSE7_REG
),
260 static const struct regmap_access_table as3722_readable_table
= {
261 .yes_ranges
= as3722_readable_ranges
,
262 .n_yes_ranges
= ARRAY_SIZE(as3722_readable_ranges
),
265 static const struct regmap_range as3722_writable_ranges
[] = {
266 regmap_reg_range(AS3722_SD0_VOLTAGE_REG
, AS3722_SD6_VOLTAGE_REG
),
267 regmap_reg_range(AS3722_GPIO0_CONTROL_REG
, AS3722_LDO7_VOLTAGE_REG
),
268 regmap_reg_range(AS3722_LDO9_VOLTAGE_REG
, AS3722_GPIO_SIGNAL_OUT_REG
),
269 regmap_reg_range(AS3722_REG_SEQU_MOD1_REG
, AS3722_REG_SEQU_MOD3_REG
),
270 regmap_reg_range(AS3722_SD_PHSW_CTRL_REG
, AS3722_PWM_CONTROL_H_REG
),
271 regmap_reg_range(AS3722_WATCHDOG_TIMER_REG
, AS3722_WATCHDOG_TIMER_REG
),
272 regmap_reg_range(AS3722_WATCHDOG_SOFTWARE_SIGNAL_REG
,
273 AS3722_BATTERY_VOLTAGE_MONITOR2_REG
),
274 regmap_reg_range(AS3722_SD_CONTROL_REG
, AS3722_PWM_VCONTROL4_REG
),
275 regmap_reg_range(AS3722_BB_CHARGER_REG
, AS3722_SRAM_REG
),
276 regmap_reg_range(AS3722_INTERRUPT_MASK1_REG
, AS3722_TEMP_STATUS_REG
),
277 regmap_reg_range(AS3722_ADC0_CONTROL_REG
, AS3722_ADC1_CONTROL_REG
),
278 regmap_reg_range(AS3722_ADC1_THRESHOLD_HI_MSB_REG
,
279 AS3722_ADC_CONFIGURATION_REG
),
280 regmap_reg_range(AS3722_LOCK_REG
, AS3722_LOCK_REG
),
283 static const struct regmap_access_table as3722_writable_table
= {
284 .yes_ranges
= as3722_writable_ranges
,
285 .n_yes_ranges
= ARRAY_SIZE(as3722_writable_ranges
),
288 static const struct regmap_range as3722_cacheable_ranges
[] = {
289 regmap_reg_range(AS3722_SD0_VOLTAGE_REG
, AS3722_LDO11_VOLTAGE_REG
),
290 regmap_reg_range(AS3722_SD_CONTROL_REG
, AS3722_LDOCONTROL1_REG
),
293 static const struct regmap_access_table as3722_volatile_table
= {
294 .no_ranges
= as3722_cacheable_ranges
,
295 .n_no_ranges
= ARRAY_SIZE(as3722_cacheable_ranges
),
298 static const struct regmap_config as3722_regmap_config
= {
301 .max_register
= AS3722_MAX_REGISTER
,
302 .cache_type
= REGCACHE_MAPLE
,
303 .rd_table
= &as3722_readable_table
,
304 .wr_table
= &as3722_writable_table
,
305 .volatile_table
= &as3722_volatile_table
,
308 static int as3722_i2c_of_probe(struct i2c_client
*i2c
,
309 struct as3722
*as3722
)
311 struct device_node
*np
= i2c
->dev
.of_node
;
312 struct irq_data
*irq_data
;
315 dev_err(&i2c
->dev
, "Device Tree not found\n");
319 irq_data
= irq_get_irq_data(i2c
->irq
);
321 dev_err(&i2c
->dev
, "Invalid IRQ: %d\n", i2c
->irq
);
325 as3722
->en_intern_int_pullup
= of_property_read_bool(np
,
326 "ams,enable-internal-int-pullup");
327 as3722
->en_intern_i2c_pullup
= of_property_read_bool(np
,
328 "ams,enable-internal-i2c-pullup");
329 as3722
->en_ac_ok_pwr_on
= of_property_read_bool(np
,
330 "ams,enable-ac-ok-power-on");
331 as3722
->irq_flags
= irqd_get_trigger_type(irq_data
);
332 dev_dbg(&i2c
->dev
, "IRQ flags are 0x%08lx\n", as3722
->irq_flags
);
336 static int as3722_i2c_probe(struct i2c_client
*i2c
)
338 struct as3722
*as3722
;
339 unsigned long irq_flags
;
343 as3722
= devm_kzalloc(&i2c
->dev
, sizeof(struct as3722
), GFP_KERNEL
);
347 as3722
->dev
= &i2c
->dev
;
348 as3722
->chip_irq
= i2c
->irq
;
349 i2c_set_clientdata(i2c
, as3722
);
351 ret
= as3722_i2c_of_probe(i2c
, as3722
);
355 as3722
->regmap
= devm_regmap_init_i2c(i2c
, &as3722_regmap_config
);
356 if (IS_ERR(as3722
->regmap
)) {
357 ret
= PTR_ERR(as3722
->regmap
);
358 dev_err(&i2c
->dev
, "regmap init failed: %d\n", ret
);
362 ret
= as3722_check_device_id(as3722
);
366 irq_flags
= as3722
->irq_flags
| IRQF_ONESHOT
;
367 ret
= devm_regmap_add_irq_chip(as3722
->dev
, as3722
->regmap
,
369 irq_flags
, -1, &as3722_irq_chip
,
372 dev_err(as3722
->dev
, "Failed to add regmap irq: %d\n", ret
);
376 ret
= as3722_configure_pullups(as3722
);
380 if (as3722
->en_ac_ok_pwr_on
)
381 val
= AS3722_CTRL_SEQU1_AC_OK_PWR_ON
;
382 ret
= as3722_update_bits(as3722
, AS3722_CTRL_SEQU1_REG
,
383 AS3722_CTRL_SEQU1_AC_OK_PWR_ON
, val
);
385 dev_err(as3722
->dev
, "CTRLsequ1 update failed: %d\n", ret
);
389 ret
= devm_mfd_add_devices(&i2c
->dev
, -1, as3722_devs
,
390 ARRAY_SIZE(as3722_devs
), NULL
, 0,
391 regmap_irq_get_domain(as3722
->irq_data
));
393 dev_err(as3722
->dev
, "Failed to add MFD devices: %d\n", ret
);
397 device_init_wakeup(as3722
->dev
, true);
399 dev_dbg(as3722
->dev
, "AS3722 core driver initialized successfully\n");
403 static int __maybe_unused
as3722_i2c_suspend(struct device
*dev
)
405 struct as3722
*as3722
= dev_get_drvdata(dev
);
407 if (device_may_wakeup(dev
))
408 enable_irq_wake(as3722
->chip_irq
);
409 disable_irq(as3722
->chip_irq
);
414 static int __maybe_unused
as3722_i2c_resume(struct device
*dev
)
416 struct as3722
*as3722
= dev_get_drvdata(dev
);
418 enable_irq(as3722
->chip_irq
);
420 if (device_may_wakeup(dev
))
421 disable_irq_wake(as3722
->chip_irq
);
426 static const struct of_device_id as3722_of_match
[] = {
427 { .compatible
= "ams,as3722", },
430 MODULE_DEVICE_TABLE(of
, as3722_of_match
);
432 static const struct i2c_device_id as3722_i2c_id
[] = {
436 MODULE_DEVICE_TABLE(i2c
, as3722_i2c_id
);
438 static const struct dev_pm_ops as3722_pm_ops
= {
439 SET_SYSTEM_SLEEP_PM_OPS(as3722_i2c_suspend
, as3722_i2c_resume
)
442 static struct i2c_driver as3722_i2c_driver
= {
445 .of_match_table
= as3722_of_match
,
446 .pm
= &as3722_pm_ops
,
448 .probe
= as3722_i2c_probe
,
449 .id_table
= as3722_i2c_id
,
452 module_i2c_driver(as3722_i2c_driver
);
454 MODULE_DESCRIPTION("I2C support for AS3722 PMICs");
455 MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
456 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
457 MODULE_LICENSE("GPL");