Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Simple MFD - I2C |
| 4 | * |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 5 | * Author(s): |
| 6 | * Michael Walle <michael@walle.cc> |
| 7 | * Lee Jones <lee.jones@linaro.org> |
| 8 | * |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 9 | * This driver creates a single register map with the intention for it to be |
| 10 | * shared by all sub-devices. Children can use their parent's device structure |
| 11 | * (dev.parent) in order to reference it. |
| 12 | * |
| 13 | * Once the register map has been successfully initialised, any sub-devices |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 14 | * represented by child nodes in Device Tree or via the MFD cells in this file |
| 15 | * will be subsequently registered. |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/kernel.h> |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 20 | #include <linux/mfd/core.h> |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/regmap.h> |
| 24 | |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 25 | #include "simple-mfd-i2c.h" |
| 26 | |
| 27 | static const struct regmap_config regmap_config_8r_8v = { |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 28 | .reg_bits = 8, |
| 29 | .val_bits = 8, |
| 30 | }; |
| 31 | |
| 32 | static int simple_mfd_i2c_probe(struct i2c_client *i2c) |
| 33 | { |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 34 | const struct simple_mfd_data *simple_mfd_data; |
| 35 | const struct regmap_config *regmap_config; |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 36 | struct regmap *regmap; |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 37 | int ret; |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 38 | |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 39 | simple_mfd_data = device_get_match_data(&i2c->dev); |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 40 | |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 41 | /* If no regmap_config is specified, use the default 8reg and 8val bits */ |
| 42 | if (!simple_mfd_data || !simple_mfd_data->regmap_config) |
| 43 | regmap_config = ®map_config_8r_8v; |
| 44 | else |
| 45 | regmap_config = simple_mfd_data->regmap_config; |
| 46 | |
| 47 | regmap = devm_regmap_init_i2c(i2c, regmap_config); |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 48 | if (IS_ERR(regmap)) |
| 49 | return PTR_ERR(regmap); |
| 50 | |
Lee Jones | 81435ed2 | 2023-01-09 17:26:36 +0000 | [diff] [blame] | 51 | /* If no MFD cells are specified, register using the DT child nodes instead */ |
Lee Jones | c753ea3 | 2021-08-04 15:21:59 +0100 | [diff] [blame] | 52 | if (!simple_mfd_data || !simple_mfd_data->mfd_cell) |
| 53 | return devm_of_platform_populate(&i2c->dev); |
| 54 | |
| 55 | ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO, |
| 56 | simple_mfd_data->mfd_cell, |
| 57 | simple_mfd_data->mfd_cell_size, |
| 58 | NULL, 0, NULL); |
| 59 | if (ret) |
| 60 | dev_err(&i2c->dev, "Failed to add child devices\n"); |
| 61 | |
| 62 | return ret; |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Alistair Francis | 5913eb4 | 2022-01-24 22:10:04 +1000 | [diff] [blame] | 65 | static const struct mfd_cell sy7636a_cells[] = { |
| 66 | { .name = "sy7636a-regulator", }, |
| 67 | { .name = "sy7636a-temperature", }, |
| 68 | }; |
| 69 | |
| 70 | static const struct simple_mfd_data silergy_sy7636a = { |
| 71 | .mfd_cell = sy7636a_cells, |
| 72 | .mfd_cell_size = ARRAY_SIZE(sy7636a_cells), |
| 73 | }; |
| 74 | |
Patrick Rudolph | 0742c2a | 2023-03-07 13:12:45 +0100 | [diff] [blame] | 75 | static const struct mfd_cell max597x_cells[] = { |
| 76 | { .name = "max597x-regulator", }, |
| 77 | { .name = "max597x-iio", }, |
| 78 | { .name = "max597x-led", }, |
| 79 | }; |
| 80 | |
| 81 | static const struct simple_mfd_data maxim_max597x = { |
| 82 | .mfd_cell = max597x_cells, |
| 83 | .mfd_cell_size = ARRAY_SIZE(max597x_cells), |
| 84 | }; |
| 85 | |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 86 | static const struct of_device_id simple_mfd_i2c_of_match[] = { |
Michael Walle | a538ad2 | 2020-09-14 23:43:31 +0200 | [diff] [blame] | 87 | { .compatible = "kontron,sl28cpld" }, |
Alistair Francis | 5913eb4 | 2022-01-24 22:10:04 +1000 | [diff] [blame] | 88 | { .compatible = "silergy,sy7636a", .data = &silergy_sy7636a}, |
Patrick Rudolph | 0742c2a | 2023-03-07 13:12:45 +0100 | [diff] [blame] | 89 | { .compatible = "maxim,max5970", .data = &maxim_max597x}, |
| 90 | { .compatible = "maxim,max5978", .data = &maxim_max597x}, |
Michael Walle | 3abee45 | 2020-09-14 23:43:29 +0200 | [diff] [blame] | 91 | {} |
| 92 | }; |
| 93 | MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match); |
| 94 | |
| 95 | static struct i2c_driver simple_mfd_i2c_driver = { |
| 96 | .probe_new = simple_mfd_i2c_probe, |
| 97 | .driver = { |
| 98 | .name = "simple-mfd-i2c", |
| 99 | .of_match_table = simple_mfd_i2c_of_match, |
| 100 | }, |
| 101 | }; |
| 102 | module_i2c_driver(simple_mfd_i2c_driver); |
| 103 | |
| 104 | MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); |
| 105 | MODULE_DESCRIPTION("Simple MFD - I2C driver"); |
| 106 | MODULE_LICENSE("GPL v2"); |