1*480a8ad6SHaibo Chen // SPDX-License-Identifier: GPL-2.0-only
2*480a8ad6SHaibo Chen /*
3*480a8ad6SHaibo Chen * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller
4*480a8ad6SHaibo Chen *
5*480a8ad6SHaibo Chen * Copyright 2022 NXP
6*480a8ad6SHaibo Chen * Copyright 2024 Ideas on Board Oy
7*480a8ad6SHaibo Chen */
8*480a8ad6SHaibo Chen
9*480a8ad6SHaibo Chen #include <linux/array_size.h>
10*480a8ad6SHaibo Chen #include <linux/device.h>
11*480a8ad6SHaibo Chen #include <linux/err.h>
12*480a8ad6SHaibo Chen #include <linux/i2c.h>
13*480a8ad6SHaibo Chen #include <linux/mfd/adp5585.h>
14*480a8ad6SHaibo Chen #include <linux/mfd/core.h>
15*480a8ad6SHaibo Chen #include <linux/mod_devicetable.h>
16*480a8ad6SHaibo Chen #include <linux/module.h>
17*480a8ad6SHaibo Chen #include <linux/regmap.h>
18*480a8ad6SHaibo Chen #include <linux/types.h>
19*480a8ad6SHaibo Chen
20*480a8ad6SHaibo Chen static const struct mfd_cell adp5585_devs[] = {
21*480a8ad6SHaibo Chen { .name = "adp5585-gpio", },
22*480a8ad6SHaibo Chen { .name = "adp5585-pwm", },
23*480a8ad6SHaibo Chen };
24*480a8ad6SHaibo Chen
25*480a8ad6SHaibo Chen static const struct regmap_range adp5585_volatile_ranges[] = {
26*480a8ad6SHaibo Chen regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B),
27*480a8ad6SHaibo Chen };
28*480a8ad6SHaibo Chen
29*480a8ad6SHaibo Chen static const struct regmap_access_table adp5585_volatile_regs = {
30*480a8ad6SHaibo Chen .yes_ranges = adp5585_volatile_ranges,
31*480a8ad6SHaibo Chen .n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges),
32*480a8ad6SHaibo Chen };
33*480a8ad6SHaibo Chen
34*480a8ad6SHaibo Chen /*
35*480a8ad6SHaibo Chen * Chip variants differ in the default configuration of pull-up and pull-down
36*480a8ad6SHaibo Chen * resistors, and therefore have different default register values:
37*480a8ad6SHaibo Chen *
38*480a8ad6SHaibo Chen * - The -00, -01 and -03 variants (collectively referred to as
39*480a8ad6SHaibo Chen * ADP5585_REGMAP_00) have pull-up on all GPIO pins by default.
40*480a8ad6SHaibo Chen * - The -02 variant has no default pull-up or pull-down resistors.
41*480a8ad6SHaibo Chen * - The -04 variant has default pull-down resistors on all GPIO pins.
42*480a8ad6SHaibo Chen */
43*480a8ad6SHaibo Chen
44*480a8ad6SHaibo Chen static const u8 adp5585_regmap_defaults_00[ADP5585_MAX_REG + 1] = {
45*480a8ad6SHaibo Chen /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46*480a8ad6SHaibo Chen /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47*480a8ad6SHaibo Chen /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48*480a8ad6SHaibo Chen /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49*480a8ad6SHaibo Chen /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50*480a8ad6SHaibo Chen /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51*480a8ad6SHaibo Chen /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52*480a8ad6SHaibo Chen /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
53*480a8ad6SHaibo Chen };
54*480a8ad6SHaibo Chen
55*480a8ad6SHaibo Chen static const u8 adp5585_regmap_defaults_02[ADP5585_MAX_REG + 1] = {
56*480a8ad6SHaibo Chen /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57*480a8ad6SHaibo Chen /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58*480a8ad6SHaibo Chen /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3,
59*480a8ad6SHaibo Chen /* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
60*480a8ad6SHaibo Chen /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61*480a8ad6SHaibo Chen /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62*480a8ad6SHaibo Chen /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63*480a8ad6SHaibo Chen /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
64*480a8ad6SHaibo Chen };
65*480a8ad6SHaibo Chen
66*480a8ad6SHaibo Chen static const u8 adp5585_regmap_defaults_04[ADP5585_MAX_REG + 1] = {
67*480a8ad6SHaibo Chen /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68*480a8ad6SHaibo Chen /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69*480a8ad6SHaibo Chen /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
70*480a8ad6SHaibo Chen /* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
71*480a8ad6SHaibo Chen /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72*480a8ad6SHaibo Chen /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73*480a8ad6SHaibo Chen /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74*480a8ad6SHaibo Chen /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
75*480a8ad6SHaibo Chen };
76*480a8ad6SHaibo Chen
77*480a8ad6SHaibo Chen enum adp5585_regmap_type {
78*480a8ad6SHaibo Chen ADP5585_REGMAP_00,
79*480a8ad6SHaibo Chen ADP5585_REGMAP_02,
80*480a8ad6SHaibo Chen ADP5585_REGMAP_04,
81*480a8ad6SHaibo Chen };
82*480a8ad6SHaibo Chen
83*480a8ad6SHaibo Chen static const struct regmap_config adp5585_regmap_configs[] = {
84*480a8ad6SHaibo Chen [ADP5585_REGMAP_00] = {
85*480a8ad6SHaibo Chen .reg_bits = 8,
86*480a8ad6SHaibo Chen .val_bits = 8,
87*480a8ad6SHaibo Chen .max_register = ADP5585_MAX_REG,
88*480a8ad6SHaibo Chen .volatile_table = &adp5585_volatile_regs,
89*480a8ad6SHaibo Chen .cache_type = REGCACHE_MAPLE,
90*480a8ad6SHaibo Chen .reg_defaults_raw = adp5585_regmap_defaults_00,
91*480a8ad6SHaibo Chen .num_reg_defaults_raw = sizeof(adp5585_regmap_defaults_00),
92*480a8ad6SHaibo Chen },
93*480a8ad6SHaibo Chen [ADP5585_REGMAP_02] = {
94*480a8ad6SHaibo Chen .reg_bits = 8,
95*480a8ad6SHaibo Chen .val_bits = 8,
96*480a8ad6SHaibo Chen .max_register = ADP5585_MAX_REG,
97*480a8ad6SHaibo Chen .volatile_table = &adp5585_volatile_regs,
98*480a8ad6SHaibo Chen .cache_type = REGCACHE_MAPLE,
99*480a8ad6SHaibo Chen .reg_defaults_raw = adp5585_regmap_defaults_02,
100*480a8ad6SHaibo Chen .num_reg_defaults_raw = sizeof(adp5585_regmap_defaults_02),
101*480a8ad6SHaibo Chen },
102*480a8ad6SHaibo Chen [ADP5585_REGMAP_04] = {
103*480a8ad6SHaibo Chen .reg_bits = 8,
104*480a8ad6SHaibo Chen .val_bits = 8,
105*480a8ad6SHaibo Chen .max_register = ADP5585_MAX_REG,
106*480a8ad6SHaibo Chen .volatile_table = &adp5585_volatile_regs,
107*480a8ad6SHaibo Chen .cache_type = REGCACHE_MAPLE,
108*480a8ad6SHaibo Chen .reg_defaults_raw = adp5585_regmap_defaults_04,
109*480a8ad6SHaibo Chen .num_reg_defaults_raw = sizeof(adp5585_regmap_defaults_04),
110*480a8ad6SHaibo Chen },
111*480a8ad6SHaibo Chen };
112*480a8ad6SHaibo Chen
adp5585_i2c_probe(struct i2c_client * i2c)113*480a8ad6SHaibo Chen static int adp5585_i2c_probe(struct i2c_client *i2c)
114*480a8ad6SHaibo Chen {
115*480a8ad6SHaibo Chen const struct regmap_config *regmap_config;
116*480a8ad6SHaibo Chen struct adp5585_dev *adp5585;
117*480a8ad6SHaibo Chen unsigned int id;
118*480a8ad6SHaibo Chen int ret;
119*480a8ad6SHaibo Chen
120*480a8ad6SHaibo Chen adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL);
121*480a8ad6SHaibo Chen if (!adp5585)
122*480a8ad6SHaibo Chen return -ENOMEM;
123*480a8ad6SHaibo Chen
124*480a8ad6SHaibo Chen i2c_set_clientdata(i2c, adp5585);
125*480a8ad6SHaibo Chen
126*480a8ad6SHaibo Chen regmap_config = i2c_get_match_data(i2c);
127*480a8ad6SHaibo Chen adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config);
128*480a8ad6SHaibo Chen if (IS_ERR(adp5585->regmap))
129*480a8ad6SHaibo Chen return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap),
130*480a8ad6SHaibo Chen "Failed to initialize register map\n");
131*480a8ad6SHaibo Chen
132*480a8ad6SHaibo Chen ret = regmap_read(adp5585->regmap, ADP5585_ID, &id);
133*480a8ad6SHaibo Chen if (ret)
134*480a8ad6SHaibo Chen return dev_err_probe(&i2c->dev, ret,
135*480a8ad6SHaibo Chen "Failed to read device ID\n");
136*480a8ad6SHaibo Chen
137*480a8ad6SHaibo Chen if ((id & ADP5585_MAN_ID_MASK) != ADP5585_MAN_ID_VALUE)
138*480a8ad6SHaibo Chen return dev_err_probe(&i2c->dev, -ENODEV,
139*480a8ad6SHaibo Chen "Invalid device ID 0x%02x\n", id);
140*480a8ad6SHaibo Chen
141*480a8ad6SHaibo Chen ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
142*480a8ad6SHaibo Chen adp5585_devs, ARRAY_SIZE(adp5585_devs),
143*480a8ad6SHaibo Chen NULL, 0, NULL);
144*480a8ad6SHaibo Chen if (ret)
145*480a8ad6SHaibo Chen return dev_err_probe(&i2c->dev, ret,
146*480a8ad6SHaibo Chen "Failed to add child devices\n");
147*480a8ad6SHaibo Chen
148*480a8ad6SHaibo Chen return 0;
149*480a8ad6SHaibo Chen }
150*480a8ad6SHaibo Chen
adp5585_suspend(struct device * dev)151*480a8ad6SHaibo Chen static int adp5585_suspend(struct device *dev)
152*480a8ad6SHaibo Chen {
153*480a8ad6SHaibo Chen struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
154*480a8ad6SHaibo Chen
155*480a8ad6SHaibo Chen regcache_cache_only(adp5585->regmap, true);
156*480a8ad6SHaibo Chen
157*480a8ad6SHaibo Chen return 0;
158*480a8ad6SHaibo Chen }
159*480a8ad6SHaibo Chen
adp5585_resume(struct device * dev)160*480a8ad6SHaibo Chen static int adp5585_resume(struct device *dev)
161*480a8ad6SHaibo Chen {
162*480a8ad6SHaibo Chen struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
163*480a8ad6SHaibo Chen
164*480a8ad6SHaibo Chen regcache_cache_only(adp5585->regmap, false);
165*480a8ad6SHaibo Chen regcache_mark_dirty(adp5585->regmap);
166*480a8ad6SHaibo Chen
167*480a8ad6SHaibo Chen return regcache_sync(adp5585->regmap);
168*480a8ad6SHaibo Chen }
169*480a8ad6SHaibo Chen
170*480a8ad6SHaibo Chen static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume);
171*480a8ad6SHaibo Chen
172*480a8ad6SHaibo Chen static const struct of_device_id adp5585_of_match[] = {
173*480a8ad6SHaibo Chen {
174*480a8ad6SHaibo Chen .compatible = "adi,adp5585-00",
175*480a8ad6SHaibo Chen .data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
176*480a8ad6SHaibo Chen }, {
177*480a8ad6SHaibo Chen .compatible = "adi,adp5585-01",
178*480a8ad6SHaibo Chen .data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
179*480a8ad6SHaibo Chen }, {
180*480a8ad6SHaibo Chen .compatible = "adi,adp5585-02",
181*480a8ad6SHaibo Chen .data = &adp5585_regmap_configs[ADP5585_REGMAP_02],
182*480a8ad6SHaibo Chen }, {
183*480a8ad6SHaibo Chen .compatible = "adi,adp5585-03",
184*480a8ad6SHaibo Chen .data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
185*480a8ad6SHaibo Chen }, {
186*480a8ad6SHaibo Chen .compatible = "adi,adp5585-04",
187*480a8ad6SHaibo Chen .data = &adp5585_regmap_configs[ADP5585_REGMAP_04],
188*480a8ad6SHaibo Chen },
189*480a8ad6SHaibo Chen { /* sentinel */ }
190*480a8ad6SHaibo Chen };
191*480a8ad6SHaibo Chen MODULE_DEVICE_TABLE(of, adp5585_of_match);
192*480a8ad6SHaibo Chen
193*480a8ad6SHaibo Chen static struct i2c_driver adp5585_i2c_driver = {
194*480a8ad6SHaibo Chen .driver = {
195*480a8ad6SHaibo Chen .name = "adp5585",
196*480a8ad6SHaibo Chen .of_match_table = adp5585_of_match,
197*480a8ad6SHaibo Chen .pm = pm_sleep_ptr(&adp5585_pm),
198*480a8ad6SHaibo Chen },
199*480a8ad6SHaibo Chen .probe = adp5585_i2c_probe,
200*480a8ad6SHaibo Chen };
201*480a8ad6SHaibo Chen module_i2c_driver(adp5585_i2c_driver);
202*480a8ad6SHaibo Chen
203*480a8ad6SHaibo Chen MODULE_DESCRIPTION("ADP5585 core driver");
204*480a8ad6SHaibo Chen MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
205*480a8ad6SHaibo Chen MODULE_LICENSE("GPL");
206