2 * Copyright (C) 2017 Spreadtrum Communications Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mfd/core.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/spi/spi.h>
22 #define SPRD_PMIC_INT_MASK_STATUS 0x0
23 #define SPRD_PMIC_INT_RAW_STATUS 0x4
24 #define SPRD_PMIC_INT_EN 0x8
26 #define SPRD_SC2731_IRQ_BASE 0x140
27 #define SPRD_SC2731_IRQ_NUMS 16
30 struct regmap
*regmap
;
32 struct regmap_irq
*irqs
;
33 struct regmap_irq_chip irq_chip
;
34 struct regmap_irq_chip_data
*irq_data
;
38 struct sprd_pmic_data
{
44 * Since different PMICs of SC27xx series can have different interrupt
45 * base address and irq number, we should save irq number and irq base
46 * in the device data structure.
48 static const struct sprd_pmic_data sc2731_data
= {
49 .irq_base
= SPRD_SC2731_IRQ_BASE
,
50 .num_irqs
= SPRD_SC2731_IRQ_NUMS
,
53 static const struct mfd_cell sprd_pmic_devs
[] = {
56 .of_compatible
= "sprd,sc27xx-wdt",
59 .of_compatible
= "sprd,sc27xx-rtc",
61 .name
= "sc27xx-charger",
62 .of_compatible
= "sprd,sc27xx-charger",
64 .name
= "sc27xx-chg-timer",
65 .of_compatible
= "sprd,sc27xx-chg-timer",
67 .name
= "sc27xx-fast-chg",
68 .of_compatible
= "sprd,sc27xx-fast-chg",
70 .name
= "sc27xx-chg-wdt",
71 .of_compatible
= "sprd,sc27xx-chg-wdt",
73 .name
= "sc27xx-typec",
74 .of_compatible
= "sprd,sc27xx-typec",
76 .name
= "sc27xx-flash",
77 .of_compatible
= "sprd,sc27xx-flash",
80 .of_compatible
= "sprd,sc27xx-eic",
82 .name
= "sc27xx-efuse",
83 .of_compatible
= "sprd,sc27xx-efuse",
85 .name
= "sc27xx-thermal",
86 .of_compatible
= "sprd,sc27xx-thermal",
89 .of_compatible
= "sprd,sc27xx-adc",
91 .name
= "sc27xx-audio-codec",
92 .of_compatible
= "sprd,sc27xx-audio-codec",
94 .name
= "sc27xx-regulator",
95 .of_compatible
= "sprd,sc27xx-regulator",
97 .name
= "sc27xx-vibrator",
98 .of_compatible
= "sprd,sc27xx-vibrator",
100 .name
= "sc27xx-keypad-led",
101 .of_compatible
= "sprd,sc27xx-keypad-led",
103 .name
= "sc27xx-bltc",
104 .of_compatible
= "sprd,sc27xx-bltc",
106 .name
= "sc27xx-fgu",
107 .of_compatible
= "sprd,sc27xx-fgu",
109 .name
= "sc27xx-7sreset",
110 .of_compatible
= "sprd,sc27xx-7sreset",
112 .name
= "sc27xx-poweroff",
113 .of_compatible
= "sprd,sc27xx-poweroff",
115 .name
= "sc27xx-syscon",
116 .of_compatible
= "sprd,sc27xx-syscon",
120 static int sprd_pmic_spi_write(void *context
, const void *data
, size_t count
)
122 struct device
*dev
= context
;
123 struct spi_device
*spi
= to_spi_device(dev
);
125 return spi_write(spi
, data
, count
);
128 static int sprd_pmic_spi_read(void *context
,
129 const void *reg
, size_t reg_size
,
130 void *val
, size_t val_size
)
132 struct device
*dev
= context
;
133 struct spi_device
*spi
= to_spi_device(dev
);
134 u32 rx_buf
[2] = { 0 };
137 /* Now we only support one PMIC register to read every time. */
138 if (reg_size
!= sizeof(u32
) || val_size
!= sizeof(u32
))
141 /* Copy address to read from into first element of SPI buffer. */
142 memcpy(rx_buf
, reg
, sizeof(u32
));
143 ret
= spi_read(spi
, rx_buf
, 1);
147 memcpy(val
, rx_buf
, val_size
);
151 static struct regmap_bus sprd_pmic_regmap
= {
152 .write
= sprd_pmic_spi_write
,
153 .read
= sprd_pmic_spi_read
,
154 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
155 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
158 static const struct regmap_config sprd_pmic_config
= {
162 .max_register
= 0xffff,
165 static int sprd_pmic_probe(struct spi_device
*spi
)
167 struct sprd_pmic
*ddata
;
168 const struct sprd_pmic_data
*pdata
;
171 pdata
= of_device_get_match_data(&spi
->dev
);
173 dev_err(&spi
->dev
, "No matching driver data found\n");
177 ddata
= devm_kzalloc(&spi
->dev
, sizeof(*ddata
), GFP_KERNEL
);
181 ddata
->regmap
= devm_regmap_init(&spi
->dev
, &sprd_pmic_regmap
,
182 &spi
->dev
, &sprd_pmic_config
);
183 if (IS_ERR(ddata
->regmap
)) {
184 ret
= PTR_ERR(ddata
->regmap
);
185 dev_err(&spi
->dev
, "Failed to allocate register map %d\n", ret
);
189 spi_set_drvdata(spi
, ddata
);
190 ddata
->dev
= &spi
->dev
;
191 ddata
->irq
= spi
->irq
;
193 ddata
->irq_chip
.name
= dev_name(&spi
->dev
);
194 ddata
->irq_chip
.status_base
=
195 pdata
->irq_base
+ SPRD_PMIC_INT_MASK_STATUS
;
196 ddata
->irq_chip
.mask_base
= pdata
->irq_base
+ SPRD_PMIC_INT_EN
;
197 ddata
->irq_chip
.ack_base
= 0;
198 ddata
->irq_chip
.num_regs
= 1;
199 ddata
->irq_chip
.num_irqs
= pdata
->num_irqs
;
200 ddata
->irq_chip
.mask_invert
= true;
202 ddata
->irqs
= devm_kcalloc(&spi
->dev
,
203 pdata
->num_irqs
, sizeof(struct regmap_irq
),
208 ddata
->irq_chip
.irqs
= ddata
->irqs
;
209 for (i
= 0; i
< pdata
->num_irqs
; i
++) {
210 ddata
->irqs
[i
].reg_offset
= i
/ pdata
->num_irqs
;
211 ddata
->irqs
[i
].mask
= BIT(i
% pdata
->num_irqs
);
214 ret
= devm_regmap_add_irq_chip(&spi
->dev
, ddata
->regmap
, ddata
->irq
,
215 IRQF_ONESHOT
| IRQF_NO_SUSPEND
, 0,
216 &ddata
->irq_chip
, &ddata
->irq_data
);
218 dev_err(&spi
->dev
, "Failed to add PMIC irq chip %d\n", ret
);
222 ret
= devm_mfd_add_devices(&spi
->dev
, PLATFORM_DEVID_AUTO
,
223 sprd_pmic_devs
, ARRAY_SIZE(sprd_pmic_devs
),
225 regmap_irq_get_domain(ddata
->irq_data
));
227 dev_err(&spi
->dev
, "Failed to register device %d\n", ret
);
234 static const struct of_device_id sprd_pmic_match
[] = {
235 { .compatible
= "sprd,sc2731", .data
= &sc2731_data
},
238 MODULE_DEVICE_TABLE(of
, sprd_pmic_match
);
240 static struct spi_driver sprd_pmic_driver
= {
242 .name
= "sc27xx-pmic",
243 .bus
= &spi_bus_type
,
244 .of_match_table
= sprd_pmic_match
,
246 .probe
= sprd_pmic_probe
,
249 static int __init
sprd_pmic_init(void)
251 return spi_register_driver(&sprd_pmic_driver
);
253 subsys_initcall(sprd_pmic_init
);
255 static void __exit
sprd_pmic_exit(void)
257 spi_unregister_driver(&sprd_pmic_driver
);
259 module_exit(sprd_pmic_exit
);
261 MODULE_LICENSE("GPL v2");
262 MODULE_DESCRIPTION("Spreadtrum SC27xx PMICs driver");
263 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");