1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DA9150 Core MFD Driver
5 * Copyright (c) 2014 Dialog Semiconductor
7 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/i2c.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/da9150/core.h>
20 #include <linux/mfd/da9150/registers.h>
22 /* Raw device access, used for QIF */
23 static int da9150_i2c_read_device(struct i2c_client
*client
, u8 addr
, int count
,
30 * Read is split into two transfers as device expects STOP/START rather
31 * than repeated start to carry out this kind of access.
35 xfer
.addr
= client
->addr
;
40 ret
= i2c_transfer(client
->adapter
, &xfer
, 1);
49 xfer
.addr
= client
->addr
;
50 xfer
.flags
= I2C_M_RD
;
54 ret
= i2c_transfer(client
->adapter
, &xfer
, 1);
63 static int da9150_i2c_write_device(struct i2c_client
*client
, u8 addr
,
64 int count
, const u8
*buf
)
70 reg_data
= kzalloc(1 + count
, GFP_KERNEL
);
75 memcpy(®_data
[1], buf
, count
);
77 /* Write address & data */
78 xfer
.addr
= client
->addr
;
83 ret
= i2c_transfer(client
->adapter
, &xfer
, 1);
93 static bool da9150_volatile_reg(struct device
*dev
, unsigned int reg
)
100 case DA9150_STATUS_D
:
101 case DA9150_STATUS_E
:
102 case DA9150_STATUS_F
:
103 case DA9150_STATUS_G
:
104 case DA9150_STATUS_H
:
105 case DA9150_STATUS_I
:
106 case DA9150_STATUS_J
:
107 case DA9150_STATUS_K
:
108 case DA9150_STATUS_L
:
109 case DA9150_STATUS_N
:
110 case DA9150_FAULT_LOG_A
:
111 case DA9150_FAULT_LOG_B
:
116 case DA9150_CONTROL_B
:
117 case DA9150_CONTROL_C
:
118 case DA9150_GPADC_MAN
:
119 case DA9150_GPADC_RES_A
:
120 case DA9150_GPADC_RES_B
:
121 case DA9150_ADETVB_CFG_C
:
122 case DA9150_ADETD_STAT
:
123 case DA9150_ADET_CMPSTAT
:
124 case DA9150_ADET_CTRL_A
:
125 case DA9150_PPR_TCTR_B
:
126 case DA9150_COREBTLD_STAT_A
:
127 case DA9150_CORE_DATA_A
:
128 case DA9150_CORE_DATA_B
:
129 case DA9150_CORE_DATA_C
:
130 case DA9150_CORE_DATA_D
:
131 case DA9150_CORE2WIRE_STAT_A
:
132 case DA9150_FW_CTRL_C
:
133 case DA9150_FG_CTRL_B
:
134 case DA9150_FW_CTRL_B
:
135 case DA9150_GPADC_CMAN
:
136 case DA9150_GPADC_CRES_A
:
137 case DA9150_GPADC_CRES_B
:
138 case DA9150_CC_ICHG_RES_A
:
139 case DA9150_CC_ICHG_RES_B
:
140 case DA9150_CC_IAVG_RES_A
:
141 case DA9150_CC_IAVG_RES_B
:
142 case DA9150_TAUX_CTRL_A
:
143 case DA9150_TAUX_VALUE_H
:
144 case DA9150_TAUX_VALUE_L
:
145 case DA9150_TBAT_RES_A
:
146 case DA9150_TBAT_RES_B
:
153 static const struct regmap_range_cfg da9150_range_cfg
[] = {
155 .range_min
= DA9150_PAGE_CON
,
156 .range_max
= DA9150_TBAT_RES_B
,
157 .selector_reg
= DA9150_PAGE_CON
,
158 .selector_mask
= DA9150_I2C_PAGE_MASK
,
159 .selector_shift
= DA9150_I2C_PAGE_SHIFT
,
165 static const struct regmap_config da9150_regmap_config
= {
168 .ranges
= da9150_range_cfg
,
169 .num_ranges
= ARRAY_SIZE(da9150_range_cfg
),
170 .max_register
= DA9150_TBAT_RES_B
,
172 .cache_type
= REGCACHE_RBTREE
,
174 .volatile_reg
= da9150_volatile_reg
,
177 void da9150_read_qif(struct da9150
*da9150
, u8 addr
, int count
, u8
*buf
)
181 ret
= da9150_i2c_read_device(da9150
->core_qif
, addr
, count
, buf
);
183 dev_err(da9150
->dev
, "Failed to read from QIF 0x%x: %d\n",
186 EXPORT_SYMBOL_GPL(da9150_read_qif
);
188 void da9150_write_qif(struct da9150
*da9150
, u8 addr
, int count
, const u8
*buf
)
192 ret
= da9150_i2c_write_device(da9150
->core_qif
, addr
, count
, buf
);
194 dev_err(da9150
->dev
, "Failed to write to QIF 0x%x: %d\n",
197 EXPORT_SYMBOL_GPL(da9150_write_qif
);
199 u8
da9150_reg_read(struct da9150
*da9150
, u16 reg
)
203 ret
= regmap_read(da9150
->regmap
, reg
, &val
);
205 dev_err(da9150
->dev
, "Failed to read from reg 0x%x: %d\n",
210 EXPORT_SYMBOL_GPL(da9150_reg_read
);
212 void da9150_reg_write(struct da9150
*da9150
, u16 reg
, u8 val
)
216 ret
= regmap_write(da9150
->regmap
, reg
, val
);
218 dev_err(da9150
->dev
, "Failed to write to reg 0x%x: %d\n",
221 EXPORT_SYMBOL_GPL(da9150_reg_write
);
223 void da9150_set_bits(struct da9150
*da9150
, u16 reg
, u8 mask
, u8 val
)
227 ret
= regmap_update_bits(da9150
->regmap
, reg
, mask
, val
);
229 dev_err(da9150
->dev
, "Failed to set bits in reg 0x%x: %d\n",
232 EXPORT_SYMBOL_GPL(da9150_set_bits
);
234 void da9150_bulk_read(struct da9150
*da9150
, u16 reg
, int count
, u8
*buf
)
238 ret
= regmap_bulk_read(da9150
->regmap
, reg
, buf
, count
);
240 dev_err(da9150
->dev
, "Failed to bulk read from reg 0x%x: %d\n",
243 EXPORT_SYMBOL_GPL(da9150_bulk_read
);
245 void da9150_bulk_write(struct da9150
*da9150
, u16 reg
, int count
, const u8
*buf
)
249 ret
= regmap_raw_write(da9150
->regmap
, reg
, buf
, count
);
251 dev_err(da9150
->dev
, "Failed to bulk write to reg 0x%x %d\n",
254 EXPORT_SYMBOL_GPL(da9150_bulk_write
);
256 static const struct regmap_irq da9150_irqs
[] = {
257 [DA9150_IRQ_VBUS
] = {
259 .mask
= DA9150_E_VBUS_MASK
,
263 .mask
= DA9150_E_CHG_MASK
,
265 [DA9150_IRQ_TCLASS
] = {
267 .mask
= DA9150_E_TCLASS_MASK
,
269 [DA9150_IRQ_TJUNC
] = {
271 .mask
= DA9150_E_TJUNC_MASK
,
273 [DA9150_IRQ_VFAULT
] = {
275 .mask
= DA9150_E_VFAULT_MASK
,
277 [DA9150_IRQ_CONF
] = {
279 .mask
= DA9150_E_CONF_MASK
,
283 .mask
= DA9150_E_DAT_MASK
,
285 [DA9150_IRQ_DTYPE
] = {
287 .mask
= DA9150_E_DTYPE_MASK
,
291 .mask
= DA9150_E_ID_MASK
,
295 .mask
= DA9150_E_ADP_MASK
,
297 [DA9150_IRQ_SESS_END
] = {
299 .mask
= DA9150_E_SESS_END_MASK
,
301 [DA9150_IRQ_SESS_VLD
] = {
303 .mask
= DA9150_E_SESS_VLD_MASK
,
307 .mask
= DA9150_E_FG_MASK
,
311 .mask
= DA9150_E_GP_MASK
,
313 [DA9150_IRQ_TBAT
] = {
315 .mask
= DA9150_E_TBAT_MASK
,
317 [DA9150_IRQ_GPIOA
] = {
319 .mask
= DA9150_E_GPIOA_MASK
,
321 [DA9150_IRQ_GPIOB
] = {
323 .mask
= DA9150_E_GPIOB_MASK
,
325 [DA9150_IRQ_GPIOC
] = {
327 .mask
= DA9150_E_GPIOC_MASK
,
329 [DA9150_IRQ_GPIOD
] = {
331 .mask
= DA9150_E_GPIOD_MASK
,
333 [DA9150_IRQ_GPADC
] = {
335 .mask
= DA9150_E_GPADC_MASK
,
337 [DA9150_IRQ_WKUP
] = {
339 .mask
= DA9150_E_WKUP_MASK
,
343 static const struct regmap_irq_chip da9150_regmap_irq_chip
= {
344 .name
= "da9150_irq",
345 .status_base
= DA9150_EVENT_E
,
346 .mask_base
= DA9150_IRQ_MASK_E
,
347 .ack_base
= DA9150_EVENT_E
,
348 .num_regs
= DA9150_NUM_IRQ_REGS
,
350 .num_irqs
= ARRAY_SIZE(da9150_irqs
),
353 static const struct resource da9150_gpadc_resources
[] = {
354 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_GPADC
, "GPADC"),
357 static const struct resource da9150_charger_resources
[] = {
358 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_CHG
, "CHG_STATUS"),
359 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_TJUNC
, "CHG_TJUNC"),
360 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_VFAULT
, "CHG_VFAULT"),
361 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_VBUS
, "CHG_VBUS"),
364 static const struct resource da9150_fg_resources
[] = {
365 DEFINE_RES_IRQ_NAMED(DA9150_IRQ_FG
, "FG"),
368 enum da9150_dev_idx
{
369 DA9150_GPADC_IDX
= 0,
374 static struct mfd_cell da9150_devs
[] = {
375 [DA9150_GPADC_IDX
] = {
376 .name
= "da9150-gpadc",
377 .of_compatible
= "dlg,da9150-gpadc",
378 .resources
= da9150_gpadc_resources
,
379 .num_resources
= ARRAY_SIZE(da9150_gpadc_resources
),
381 [DA9150_CHARGER_IDX
] = {
382 .name
= "da9150-charger",
383 .of_compatible
= "dlg,da9150-charger",
384 .resources
= da9150_charger_resources
,
385 .num_resources
= ARRAY_SIZE(da9150_charger_resources
),
388 .name
= "da9150-fuel-gauge",
389 .of_compatible
= "dlg,da9150-fuel-gauge",
390 .resources
= da9150_fg_resources
,
391 .num_resources
= ARRAY_SIZE(da9150_fg_resources
),
395 static int da9150_probe(struct i2c_client
*client
,
396 const struct i2c_device_id
*id
)
398 struct da9150
*da9150
;
399 struct da9150_pdata
*pdata
= dev_get_platdata(&client
->dev
);
403 da9150
= devm_kzalloc(&client
->dev
, sizeof(*da9150
), GFP_KERNEL
);
407 da9150
->dev
= &client
->dev
;
408 da9150
->irq
= client
->irq
;
409 i2c_set_clientdata(client
, da9150
);
411 da9150
->regmap
= devm_regmap_init_i2c(client
, &da9150_regmap_config
);
412 if (IS_ERR(da9150
->regmap
)) {
413 ret
= PTR_ERR(da9150
->regmap
);
414 dev_err(da9150
->dev
, "Failed to allocate register map: %d\n",
419 /* Setup secondary I2C interface for QIF access */
420 qif_addr
= da9150_reg_read(da9150
, DA9150_CORE2WIRE_CTRL_A
);
421 qif_addr
= (qif_addr
& DA9150_CORE_BASE_ADDR_MASK
) >> 1;
422 qif_addr
|= DA9150_QIF_I2C_ADDR_LSB
;
423 da9150
->core_qif
= i2c_new_dummy_device(client
->adapter
, qif_addr
);
424 if (IS_ERR(da9150
->core_qif
)) {
425 dev_err(da9150
->dev
, "Failed to attach QIF client\n");
426 return PTR_ERR(da9150
->core_qif
);
429 i2c_set_clientdata(da9150
->core_qif
, da9150
);
432 da9150
->irq_base
= pdata
->irq_base
;
434 da9150_devs
[DA9150_FG_IDX
].platform_data
= pdata
->fg_pdata
;
435 da9150_devs
[DA9150_FG_IDX
].pdata_size
=
436 sizeof(struct da9150_fg_pdata
);
438 da9150
->irq_base
= -1;
441 ret
= regmap_add_irq_chip(da9150
->regmap
, da9150
->irq
,
442 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
443 da9150
->irq_base
, &da9150_regmap_irq_chip
,
444 &da9150
->regmap_irq_data
);
446 dev_err(da9150
->dev
, "Failed to add regmap irq chip: %d\n",
448 goto regmap_irq_fail
;
452 da9150
->irq_base
= regmap_irq_chip_get_base(da9150
->regmap_irq_data
);
454 enable_irq_wake(da9150
->irq
);
456 ret
= mfd_add_devices(da9150
->dev
, -1, da9150_devs
,
457 ARRAY_SIZE(da9150_devs
), NULL
,
458 da9150
->irq_base
, NULL
);
460 dev_err(da9150
->dev
, "Failed to add child devices: %d\n", ret
);
467 regmap_del_irq_chip(da9150
->irq
, da9150
->regmap_irq_data
);
469 i2c_unregister_device(da9150
->core_qif
);
474 static int da9150_remove(struct i2c_client
*client
)
476 struct da9150
*da9150
= i2c_get_clientdata(client
);
478 regmap_del_irq_chip(da9150
->irq
, da9150
->regmap_irq_data
);
479 mfd_remove_devices(da9150
->dev
);
480 i2c_unregister_device(da9150
->core_qif
);
485 static void da9150_shutdown(struct i2c_client
*client
)
487 struct da9150
*da9150
= i2c_get_clientdata(client
);
489 /* Make sure we have a wakup source for the device */
490 da9150_set_bits(da9150
, DA9150_CONFIG_D
,
491 DA9150_WKUP_PM_EN_MASK
,
492 DA9150_WKUP_PM_EN_MASK
);
494 /* Set device to DISABLED mode */
495 da9150_set_bits(da9150
, DA9150_CONTROL_C
,
496 DA9150_DISABLE_MASK
, DA9150_DISABLE_MASK
);
499 static const struct i2c_device_id da9150_i2c_id
[] = {
503 MODULE_DEVICE_TABLE(i2c
, da9150_i2c_id
);
505 static const struct of_device_id da9150_of_match
[] = {
506 { .compatible
= "dlg,da9150", },
509 MODULE_DEVICE_TABLE(of
, da9150_of_match
);
511 static struct i2c_driver da9150_driver
= {
514 .of_match_table
= da9150_of_match
,
516 .probe
= da9150_probe
,
517 .remove
= da9150_remove
,
518 .shutdown
= da9150_shutdown
,
519 .id_table
= da9150_i2c_id
,
522 module_i2c_driver(da9150_driver
);
524 MODULE_DESCRIPTION("MFD Core Driver for DA9150");
525 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
526 MODULE_LICENSE("GPL");