2 * Motorola CPCAP PMIC core driver
4 * Copyright (C) 2016 Tony Lindgren <tony@atomide.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 #include <linux/sysfs.h>
21 #include <linux/mfd/core.h>
22 #include <linux/mfd/motorola-cpcap.h>
23 #include <linux/spi/spi.h>
25 #define CPCAP_NR_IRQ_REG_BANKS 6
26 #define CPCAP_NR_IRQ_CHIPS 3
27 #define CPCAP_REGISTER_SIZE 4
28 #define CPCAP_REGISTER_BITS 16
31 struct spi_device
*spi
;
32 struct regmap_irq
*irqs
;
33 struct regmap_irq_chip_data
*irqdata
[CPCAP_NR_IRQ_CHIPS
];
34 const struct regmap_config
*regmap_conf
;
35 struct regmap
*regmap
;
38 static int cpcap_sense_irq(struct regmap
*regmap
, int irq
)
40 int regnum
= irq
/ CPCAP_REGISTER_BITS
;
41 int mask
= BIT(irq
% CPCAP_REGISTER_BITS
);
42 int reg
= CPCAP_REG_INTS1
+ (regnum
* CPCAP_REGISTER_SIZE
);
45 if (reg
< CPCAP_REG_INTS1
|| reg
> CPCAP_REG_INTS4
)
48 err
= regmap_read(regmap
, reg
, &val
);
52 return !!(val
& mask
);
55 int cpcap_sense_virq(struct regmap
*regmap
, int virq
)
57 struct regmap_irq_chip_data
*d
= irq_get_chip_data(virq
);
58 int irq_base
= regmap_irq_chip_get_base(d
);
60 return cpcap_sense_irq(regmap
, virq
- irq_base
);
62 EXPORT_SYMBOL_GPL(cpcap_sense_virq
);
64 static int cpcap_check_revision(struct cpcap_ddata
*cpcap
)
69 ret
= cpcap_get_vendor(&cpcap
->spi
->dev
, cpcap
->regmap
, &vendor
);
73 ret
= cpcap_get_revision(&cpcap
->spi
->dev
, cpcap
->regmap
, &rev
);
77 dev_info(&cpcap
->spi
->dev
, "CPCAP vendor: %s rev: %i.%i (%x)\n",
78 vendor
== CPCAP_VENDOR_ST
? "ST" : "TI",
79 CPCAP_REVISION_MAJOR(rev
), CPCAP_REVISION_MINOR(rev
),
82 if (rev
< CPCAP_REVISION_2_1
) {
83 dev_info(&cpcap
->spi
->dev
,
84 "Please add old CPCAP revision support as needed\n");
92 * First two irq chips are the two private macro interrupt chips, the third
93 * irq chip is for register banks 1 - 4 and is available for drivers to use.
95 static struct regmap_irq_chip cpcap_irq_chip
[CPCAP_NR_IRQ_CHIPS
] = {
99 .status_base
= CPCAP_REG_MI1
,
100 .ack_base
= CPCAP_REG_MI1
,
101 .mask_base
= CPCAP_REG_MIM1
,
108 .status_base
= CPCAP_REG_MI2
,
109 .ack_base
= CPCAP_REG_MI2
,
110 .mask_base
= CPCAP_REG_MIM2
,
117 .status_base
= CPCAP_REG_INT1
,
118 .ack_base
= CPCAP_REG_INT1
,
119 .mask_base
= CPCAP_REG_INTM1
,
125 static void cpcap_init_one_regmap_irq(struct cpcap_ddata
*cpcap
,
126 struct regmap_irq
*rirq
,
127 int irq_base
, int irq
)
129 unsigned int reg_offset
;
130 unsigned int bit
, mask
;
132 reg_offset
= irq
- irq_base
;
133 reg_offset
/= cpcap
->regmap_conf
->val_bits
;
134 reg_offset
*= cpcap
->regmap_conf
->reg_stride
;
136 bit
= irq
% cpcap
->regmap_conf
->val_bits
;
139 rirq
->reg_offset
= reg_offset
;
143 static int cpcap_init_irq_chip(struct cpcap_ddata
*cpcap
, int irq_chip
,
144 int irq_start
, int nr_irqs
)
146 struct regmap_irq_chip
*chip
= &cpcap_irq_chip
[irq_chip
];
149 for (i
= irq_start
; i
< irq_start
+ nr_irqs
; i
++) {
150 struct regmap_irq
*rirq
= &cpcap
->irqs
[i
];
152 cpcap_init_one_regmap_irq(cpcap
, rirq
, irq_start
, i
);
154 chip
->irqs
= &cpcap
->irqs
[irq_start
];
155 chip
->num_irqs
= nr_irqs
;
156 chip
->irq_drv_data
= cpcap
;
158 ret
= devm_regmap_add_irq_chip(&cpcap
->spi
->dev
, cpcap
->regmap
,
160 irq_get_trigger_type(cpcap
->spi
->irq
) |
162 chip
, &cpcap
->irqdata
[irq_chip
]);
164 dev_err(&cpcap
->spi
->dev
, "could not add irq chip %i: %i\n",
172 static int cpcap_init_irq(struct cpcap_ddata
*cpcap
)
176 cpcap
->irqs
= devm_kzalloc(&cpcap
->spi
->dev
,
177 array3_size(sizeof(*cpcap
->irqs
),
178 CPCAP_NR_IRQ_REG_BANKS
,
179 cpcap
->regmap_conf
->val_bits
),
184 ret
= cpcap_init_irq_chip(cpcap
, 0, 0, 16);
188 ret
= cpcap_init_irq_chip(cpcap
, 1, 16, 16);
192 ret
= cpcap_init_irq_chip(cpcap
, 2, 32, 64);
196 enable_irq_wake(cpcap
->spi
->irq
);
201 static const struct of_device_id cpcap_of_match
[] = {
202 { .compatible
= "motorola,cpcap", },
203 { .compatible
= "st,6556002", },
206 MODULE_DEVICE_TABLE(of
, cpcap_of_match
);
208 static const struct regmap_config cpcap_regmap_config
= {
213 .write_flag_mask
= 0x8000,
214 .max_register
= CPCAP_REG_ST_TEST2
,
215 .cache_type
= REGCACHE_NONE
,
216 .reg_format_endian
= REGMAP_ENDIAN_LITTLE
,
217 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
220 static const struct mfd_cell cpcap_mfd_devices
[] = {
223 .of_compatible
= "motorola,mapphone-cpcap-adc",
225 .name
= "cpcap_battery",
226 .of_compatible
= "motorola,cpcap-battery",
228 .name
= "cpcap-charger",
229 .of_compatible
= "motorola,mapphone-cpcap-charger",
231 .name
= "cpcap-regulator",
232 .of_compatible
= "motorola,mapphone-cpcap-regulator",
235 .of_compatible
= "motorola,cpcap-rtc",
237 .name
= "cpcap-pwrbutton",
238 .of_compatible
= "motorola,cpcap-pwrbutton",
240 .name
= "cpcap-usb-phy",
241 .of_compatible
= "motorola,mapphone-cpcap-usb-phy",
245 .of_compatible
= "motorola,cpcap-led-red",
249 .of_compatible
= "motorola,cpcap-led-green",
253 .of_compatible
= "motorola,cpcap-led-blue",
257 .of_compatible
= "motorola,cpcap-led-adl",
261 .of_compatible
= "motorola,cpcap-led-cp",
263 .name
= "cpcap-codec",
267 static int cpcap_probe(struct spi_device
*spi
)
269 const struct of_device_id
*match
;
270 struct cpcap_ddata
*cpcap
;
273 match
= of_match_device(of_match_ptr(cpcap_of_match
), &spi
->dev
);
277 cpcap
= devm_kzalloc(&spi
->dev
, sizeof(*cpcap
), GFP_KERNEL
);
282 spi_set_drvdata(spi
, cpcap
);
284 spi
->bits_per_word
= 16;
285 spi
->mode
= SPI_MODE_0
| SPI_CS_HIGH
;
287 ret
= spi_setup(spi
);
291 cpcap
->regmap_conf
= &cpcap_regmap_config
;
292 cpcap
->regmap
= devm_regmap_init_spi(spi
, &cpcap_regmap_config
);
293 if (IS_ERR(cpcap
->regmap
)) {
294 ret
= PTR_ERR(cpcap
->regmap
);
295 dev_err(&cpcap
->spi
->dev
, "Failed to initialize regmap: %d\n",
301 ret
= cpcap_check_revision(cpcap
);
303 dev_err(&cpcap
->spi
->dev
, "Failed to detect CPCAP: %i\n", ret
);
307 ret
= cpcap_init_irq(cpcap
);
311 return devm_mfd_add_devices(&spi
->dev
, 0, cpcap_mfd_devices
,
312 ARRAY_SIZE(cpcap_mfd_devices
), NULL
, 0, NULL
);
315 static struct spi_driver cpcap_driver
= {
317 .name
= "cpcap-core",
318 .of_match_table
= cpcap_of_match
,
320 .probe
= cpcap_probe
,
322 module_spi_driver(cpcap_driver
);
324 MODULE_ALIAS("platform:cpcap");
325 MODULE_DESCRIPTION("CPCAP driver");
326 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
327 MODULE_LICENSE("GPL v2");