Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.9.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller
  4 *
  5 * Copyright 2022 NXP
  6 * Copyright 2024 Ideas on Board Oy
  7 */
  8
  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>
 19
 20static const struct mfd_cell adp5585_devs[] = {
 21	{ .name = "adp5585-gpio", },
 22	{ .name = "adp5585-pwm", },
 23};
 24
 25static const struct regmap_range adp5585_volatile_ranges[] = {
 26	regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B),
 27};
 28
 29static const struct regmap_access_table adp5585_volatile_regs = {
 30	.yes_ranges = adp5585_volatile_ranges,
 31	.n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges),
 32};
 33
 34/*
 35 * Chip variants differ in the default configuration of pull-up and pull-down
 36 * resistors, and therefore have different default register values:
 37 *
 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.
 42 */
 43
 44static 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,
 53};
 54
 55static 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,
 64};
 65
 66static 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,
 75};
 76
 77enum adp5585_regmap_type {
 78	ADP5585_REGMAP_00,
 79	ADP5585_REGMAP_02,
 80	ADP5585_REGMAP_04,
 81};
 82
 83static const struct regmap_config adp5585_regmap_configs[] = {
 84	[ADP5585_REGMAP_00] = {
 85		.reg_bits = 8,
 86		.val_bits = 8,
 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),
 92	},
 93	[ADP5585_REGMAP_02] = {
 94		.reg_bits = 8,
 95		.val_bits = 8,
 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),
101	},
102	[ADP5585_REGMAP_04] = {
103		.reg_bits = 8,
104		.val_bits = 8,
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),
110	},
111};
112
113static int adp5585_i2c_probe(struct i2c_client *i2c)
114{
115	const struct regmap_config *regmap_config;
116	struct adp5585_dev *adp5585;
117	unsigned int id;
118	int ret;
119
120	adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL);
121	if (!adp5585)
122		return -ENOMEM;
123
124	i2c_set_clientdata(i2c, adp5585);
125
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");
131
132	ret = regmap_read(adp5585->regmap, ADP5585_ID, &id);
133	if (ret)
134		return dev_err_probe(&i2c->dev, ret,
135				     "Failed to read device ID\n");
136
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);
140
141	ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
142				   adp5585_devs, ARRAY_SIZE(adp5585_devs),
143				   NULL, 0, NULL);
144	if (ret)
145		return dev_err_probe(&i2c->dev, ret,
146				     "Failed to add child devices\n");
147
148	return 0;
149}
150
151static int adp5585_suspend(struct device *dev)
152{
153	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
154
155	regcache_cache_only(adp5585->regmap, true);
156
157	return 0;
158}
159
160static int adp5585_resume(struct device *dev)
161{
162	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
163
164	regcache_cache_only(adp5585->regmap, false);
165	regcache_mark_dirty(adp5585->regmap);
166
167	return regcache_sync(adp5585->regmap);
168}
169
170static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume);
171
172static const struct of_device_id adp5585_of_match[] = {
173	{
174		.compatible = "adi,adp5585-00",
175		.data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
176	}, {
177		.compatible = "adi,adp5585-01",
178		.data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
179	}, {
180		.compatible = "adi,adp5585-02",
181		.data = &adp5585_regmap_configs[ADP5585_REGMAP_02],
182	}, {
183		.compatible = "adi,adp5585-03",
184		.data = &adp5585_regmap_configs[ADP5585_REGMAP_00],
185	}, {
186		.compatible = "adi,adp5585-04",
187		.data = &adp5585_regmap_configs[ADP5585_REGMAP_04],
188	},
189	{ /* sentinel */ }
190};
191MODULE_DEVICE_TABLE(of, adp5585_of_match);
192
193static struct i2c_driver adp5585_i2c_driver = {
194	.driver = {
195		.name = "adp5585",
196		.of_match_table = adp5585_of_match,
197		.pm = pm_sleep_ptr(&adp5585_pm),
198	},
199	.probe = adp5585_i2c_probe,
200};
201module_i2c_driver(adp5585_i2c_driver);
202
203MODULE_DESCRIPTION("ADP5585 core driver");
204MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
205MODULE_LICENSE("GPL");