| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (c) 2023 Analog Devices, Inc. |
| 4 |
* Driver for the DS4520 I/O Expander |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/device.h> |
| 8 |
#include <linux/gpio/driver.h> |
| 9 |
#include <linux/gpio/regmap.h> |
| 10 |
#include <linux/i2c.h> |
| 11 |
#include <linux/property.h> |
| 12 |
#include <linux/regmap.h> |
| 13 |
|
| 14 |
#define DS4520_PULLUP0 0xF0 |
| 15 |
#define DS4520_IO_CONTROL0 0xF2 |
| 16 |
#define DS4520_IO_STATUS0 0xF8 |
| 17 |
|
| 18 |
static const struct regmap_config ds4520_regmap_config = { |
| 19 |
.reg_bits = 8, |
| 20 |
.val_bits = 8, |
| 21 |
}; |
| 22 |
|
| 23 |
static int ds4520_gpio_probe(struct i2c_client *client) |
| 24 |
{ |
| 25 |
struct gpio_regmap_config config = { }; |
| 26 |
struct device *dev = &client->dev; |
| 27 |
struct regmap *regmap; |
| 28 |
u32 base; |
| 29 |
int ret; |
| 30 |
|
| 31 |
ret = device_property_read_u32(dev, "reg", &base); |
| 32 |
if (ret) |
| 33 |
return dev_err_probe(dev, ret, "Missing 'reg' property.\n"); |
| 34 |
|
| 35 |
regmap = devm_regmap_init_i2c(client, &ds4520_regmap_config); |
| 36 |
if (IS_ERR(regmap)) |
| 37 |
return dev_err_probe(dev, PTR_ERR(regmap), |
| 38 |
"Failed to allocate register map\n"); |
| 39 |
|
| 40 |
config.regmap = regmap; |
| 41 |
config.parent = dev; |
| 42 |
|
| 43 |
config.reg_dat_base = base + DS4520_IO_STATUS0; |
| 44 |
config.reg_set_base = base + DS4520_PULLUP0; |
| 45 |
config.reg_dir_out_base = base + DS4520_IO_CONTROL0; |
| 46 |
|
| 47 |
return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &config)); |
| 48 |
} |
| 49 |
|
| 50 |
static const struct of_device_id ds4520_gpio_of_match_table[] = { |
| 51 |
{ .compatible = "adi,ds4520-gpio" }, |
| 52 |
{ } |
| 53 |
}; |
| 54 |
MODULE_DEVICE_TABLE(of, ds4520_gpio_of_match_table); |
| 55 |
|
| 56 |
static const struct i2c_device_id ds4520_gpio_id_table[] = { |
| 57 |
{ "ds4520-gpio" }, |
| 58 |
{ } |
| 59 |
}; |
| 60 |
MODULE_DEVICE_TABLE(i2c, ds4520_gpio_id_table); |
| 61 |
|
| 62 |
static struct i2c_driver ds4520_gpio_driver = { |
| 63 |
.driver = { |
| 64 |
.name = "ds4520-gpio", |
| 65 |
.of_match_table = ds4520_gpio_of_match_table, |
| 66 |
}, |
| 67 |
.probe = ds4520_gpio_probe, |
| 68 |
.id_table = ds4520_gpio_id_table, |
| 69 |
}; |
| 70 |
module_i2c_driver(ds4520_gpio_driver); |
| 71 |
|
| 72 |
MODULE_DESCRIPTION("DS4520 I/O Expander"); |
| 73 |
MODULE_AUTHOR("Okan Sahin <[email protected]>"); |
| 74 |
MODULE_LICENSE("GPL"); |