1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller
6 * Copyright 2024 Ideas on Board Oy
9 #include <linux/array_size.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/mfd/adp5585.h>
14 #include <linux/mfd/core.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/regmap.h>
18 #include <linux/types.h>
20 static const struct mfd_cell adp5585_devs
[] = {
21 { .name
= "adp5585-gpio", },
22 { .name
= "adp5585-pwm", },
25 static const struct regmap_range adp5585_volatile_ranges
[] = {
26 regmap_reg_range(ADP5585_ID
, ADP5585_GPI_STATUS_B
),
29 static const struct regmap_access_table adp5585_volatile_regs
= {
30 .yes_ranges
= adp5585_volatile_ranges
,
31 .n_yes_ranges
= ARRAY_SIZE(adp5585_volatile_ranges
),
35 * Chip variants differ in the default configuration of pull-up and pull-down
36 * resistors, and therefore have different default register values:
38 * - The -00, -01 and -03 variants (collectively referred to as
39 * ADP5585_REGMAP_00) have pull-up on all GPIO pins by default.
40 * - The -02 variant has no default pull-up or pull-down resistors.
41 * - The -04 variant has default pull-down resistors on all GPIO pins.
44 static const u8 adp5585_regmap_defaults_00
[ADP5585_MAX_REG
+ 1] = {
45 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 /* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
55 static const u8 adp5585_regmap_defaults_02
[ADP5585_MAX_REG
+ 1] = {
56 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3,
59 /* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
60 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
66 static const u8 adp5585_regmap_defaults_04
[ADP5585_MAX_REG
+ 1] = {
67 /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 /* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
70 /* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
71 /* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 /* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 /* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 /* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
77 enum adp5585_regmap_type
{
83 static const struct regmap_config adp5585_regmap_configs
[] = {
84 [ADP5585_REGMAP_00
] = {
87 .max_register
= ADP5585_MAX_REG
,
88 .volatile_table
= &adp5585_volatile_regs
,
89 .cache_type
= REGCACHE_MAPLE
,
90 .reg_defaults_raw
= adp5585_regmap_defaults_00
,
91 .num_reg_defaults_raw
= sizeof(adp5585_regmap_defaults_00
),
93 [ADP5585_REGMAP_02
] = {
96 .max_register
= ADP5585_MAX_REG
,
97 .volatile_table
= &adp5585_volatile_regs
,
98 .cache_type
= REGCACHE_MAPLE
,
99 .reg_defaults_raw
= adp5585_regmap_defaults_02
,
100 .num_reg_defaults_raw
= sizeof(adp5585_regmap_defaults_02
),
102 [ADP5585_REGMAP_04
] = {
105 .max_register
= ADP5585_MAX_REG
,
106 .volatile_table
= &adp5585_volatile_regs
,
107 .cache_type
= REGCACHE_MAPLE
,
108 .reg_defaults_raw
= adp5585_regmap_defaults_04
,
109 .num_reg_defaults_raw
= sizeof(adp5585_regmap_defaults_04
),
113 static int adp5585_i2c_probe(struct i2c_client
*i2c
)
115 const struct regmap_config
*regmap_config
;
116 struct adp5585_dev
*adp5585
;
120 adp5585
= devm_kzalloc(&i2c
->dev
, sizeof(*adp5585
), GFP_KERNEL
);
124 i2c_set_clientdata(i2c
, adp5585
);
126 regmap_config
= i2c_get_match_data(i2c
);
127 adp5585
->regmap
= devm_regmap_init_i2c(i2c
, regmap_config
);
128 if (IS_ERR(adp5585
->regmap
))
129 return dev_err_probe(&i2c
->dev
, PTR_ERR(adp5585
->regmap
),
130 "Failed to initialize register map\n");
132 ret
= regmap_read(adp5585
->regmap
, ADP5585_ID
, &id
);
134 return dev_err_probe(&i2c
->dev
, ret
,
135 "Failed to read device ID\n");
137 if ((id
& ADP5585_MAN_ID_MASK
) != ADP5585_MAN_ID_VALUE
)
138 return dev_err_probe(&i2c
->dev
, -ENODEV
,
139 "Invalid device ID 0x%02x\n", id
);
141 ret
= devm_mfd_add_devices(&i2c
->dev
, PLATFORM_DEVID_AUTO
,
142 adp5585_devs
, ARRAY_SIZE(adp5585_devs
),
145 return dev_err_probe(&i2c
->dev
, ret
,
146 "Failed to add child devices\n");
151 static int adp5585_suspend(struct device
*dev
)
153 struct adp5585_dev
*adp5585
= dev_get_drvdata(dev
);
155 regcache_cache_only(adp5585
->regmap
, true);
160 static int adp5585_resume(struct device
*dev
)
162 struct adp5585_dev
*adp5585
= dev_get_drvdata(dev
);
164 regcache_cache_only(adp5585
->regmap
, false);
165 regcache_mark_dirty(adp5585
->regmap
);
167 return regcache_sync(adp5585
->regmap
);
170 static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm
, adp5585_suspend
, adp5585_resume
);
172 static const struct of_device_id adp5585_of_match
[] = {
174 .compatible
= "adi,adp5585-00",
175 .data
= &adp5585_regmap_configs
[ADP5585_REGMAP_00
],
177 .compatible
= "adi,adp5585-01",
178 .data
= &adp5585_regmap_configs
[ADP5585_REGMAP_00
],
180 .compatible
= "adi,adp5585-02",
181 .data
= &adp5585_regmap_configs
[ADP5585_REGMAP_02
],
183 .compatible
= "adi,adp5585-03",
184 .data
= &adp5585_regmap_configs
[ADP5585_REGMAP_00
],
186 .compatible
= "adi,adp5585-04",
187 .data
= &adp5585_regmap_configs
[ADP5585_REGMAP_04
],
191 MODULE_DEVICE_TABLE(of
, adp5585_of_match
);
193 static struct i2c_driver adp5585_i2c_driver
= {
196 .of_match_table
= adp5585_of_match
,
197 .pm
= pm_sleep_ptr(&adp5585_pm
),
199 .probe
= adp5585_i2c_probe
,
201 module_i2c_driver(adp5585_i2c_driver
);
203 MODULE_DESCRIPTION("ADP5585 core driver");
204 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
205 MODULE_LICENSE("GPL");