Commit | Line | Data |
---|---|---|
2aec85b2 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
51bd6943 MV |
2 | /* |
3 | * isl6271a-regulator.c | |
4 | * | |
5 | * Support for Intersil ISL6271A voltage regulator | |
6 | * | |
7 | * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com> | |
51bd6943 MV |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/platform_device.h> | |
15 | #include <linux/regulator/driver.h> | |
16 | #include <linux/i2c.h> | |
51bd6943 MV |
17 | #include <linux/slab.h> |
18 | ||
19 | #define ISL6271A_VOLTAGE_MIN 850000 | |
20 | #define ISL6271A_VOLTAGE_MAX 1600000 | |
21 | #define ISL6271A_VOLTAGE_STEP 50000 | |
22 | ||
23 | /* PMIC details */ | |
24 | struct isl_pmic { | |
25 | struct i2c_client *client; | |
51bd6943 MV |
26 | struct mutex mtx; |
27 | }; | |
28 | ||
a9b2899b | 29 | static int isl6271a_get_voltage_sel(struct regulator_dev *dev) |
51bd6943 MV |
30 | { |
31 | struct isl_pmic *pmic = rdev_get_drvdata(dev); | |
a9b2899b | 32 | int idx; |
51bd6943 MV |
33 | |
34 | mutex_lock(&pmic->mtx); | |
35 | ||
36 | idx = i2c_smbus_read_byte(pmic->client); | |
a9b2899b | 37 | if (idx < 0) |
51bd6943 | 38 | dev_err(&pmic->client->dev, "Error getting voltage\n"); |
51bd6943 | 39 | |
51bd6943 | 40 | mutex_unlock(&pmic->mtx); |
a9b2899b | 41 | return idx; |
51bd6943 MV |
42 | } |
43 | ||
dde2b724 AL |
44 | static int isl6271a_set_voltage_sel(struct regulator_dev *dev, |
45 | unsigned selector) | |
51bd6943 MV |
46 | { |
47 | struct isl_pmic *pmic = rdev_get_drvdata(dev); | |
dde2b724 | 48 | int err; |
3a93f2a9 | 49 | |
51bd6943 MV |
50 | mutex_lock(&pmic->mtx); |
51 | ||
dde2b724 | 52 | err = i2c_smbus_write_byte(pmic->client, selector); |
51bd6943 MV |
53 | if (err < 0) |
54 | dev_err(&pmic->client->dev, "Error setting voltage\n"); | |
55 | ||
56 | mutex_unlock(&pmic->mtx); | |
57 | return err; | |
58 | } | |
59 | ||
1dceee5e | 60 | static const struct regulator_ops isl_core_ops = { |
a9b2899b | 61 | .get_voltage_sel = isl6271a_get_voltage_sel, |
dde2b724 | 62 | .set_voltage_sel = isl6271a_set_voltage_sel, |
2b7a7a4e | 63 | .list_voltage = regulator_list_voltage_linear, |
dde2b724 | 64 | .map_voltage = regulator_map_voltage_linear, |
51bd6943 MV |
65 | }; |
66 | ||
1dceee5e | 67 | static const struct regulator_ops isl_fixed_ops = { |
985f769c | 68 | .list_voltage = regulator_list_voltage_linear, |
51bd6943 MV |
69 | }; |
70 | ||
4b65e159 | 71 | static const struct regulator_desc isl_rd[] = { |
51bd6943 MV |
72 | { |
73 | .name = "Core Buck", | |
74 | .id = 0, | |
75 | .n_voltages = 16, | |
76 | .ops = &isl_core_ops, | |
77 | .type = REGULATOR_VOLTAGE, | |
78 | .owner = THIS_MODULE, | |
2b7a7a4e AL |
79 | .min_uV = ISL6271A_VOLTAGE_MIN, |
80 | .uV_step = ISL6271A_VOLTAGE_STEP, | |
51bd6943 MV |
81 | }, { |
82 | .name = "LDO1", | |
83 | .id = 1, | |
84 | .n_voltages = 1, | |
85 | .ops = &isl_fixed_ops, | |
86 | .type = REGULATOR_VOLTAGE, | |
87 | .owner = THIS_MODULE, | |
985f769c | 88 | .min_uV = 1100000, |
51bd6943 MV |
89 | }, { |
90 | .name = "LDO2", | |
91 | .id = 2, | |
92 | .n_voltages = 1, | |
93 | .ops = &isl_fixed_ops, | |
94 | .type = REGULATOR_VOLTAGE, | |
95 | .owner = THIS_MODULE, | |
985f769c | 96 | .min_uV = 1300000, |
51bd6943 MV |
97 | }, |
98 | }; | |
99 | ||
69518d21 | 100 | static int isl6271a_probe(struct i2c_client *i2c) |
51bd6943 | 101 | { |
69518d21 | 102 | const struct i2c_device_id *id = i2c_client_get_device_id(i2c); |
a6e58299 | 103 | struct regulator_dev *rdev; |
c172708d | 104 | struct regulator_config config = { }; |
dff91d0b | 105 | struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev); |
51bd6943 | 106 | struct isl_pmic *pmic; |
0ab6e8ca | 107 | int i; |
51bd6943 MV |
108 | |
109 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
110 | return -EIO; | |
111 | ||
ef6bd5a3 | 112 | pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL); |
51bd6943 MV |
113 | if (!pmic) |
114 | return -ENOMEM; | |
115 | ||
116 | pmic->client = i2c; | |
117 | ||
118 | mutex_init(&pmic->mtx); | |
119 | ||
120 | for (i = 0; i < 3; i++) { | |
c172708d MB |
121 | config.dev = &i2c->dev; |
122 | if (i == 0) | |
123 | config.init_data = init_data; | |
124 | else | |
85bea1bf | 125 | config.init_data = NULL; |
c172708d MB |
126 | config.driver_data = pmic; |
127 | ||
a6e58299 AL |
128 | rdev = devm_regulator_register(&i2c->dev, &isl_rd[i], &config); |
129 | if (IS_ERR(rdev)) { | |
51bd6943 | 130 | dev_err(&i2c->dev, "failed to register %s\n", id->name); |
a6e58299 | 131 | return PTR_ERR(rdev); |
51bd6943 MV |
132 | } |
133 | } | |
134 | ||
135 | i2c_set_clientdata(i2c, pmic); | |
136 | ||
137 | return 0; | |
51bd6943 MV |
138 | } |
139 | ||
140 | static const struct i2c_device_id isl6271a_id[] = { | |
141 | {.name = "isl6271a", 0 }, | |
142 | { }, | |
143 | }; | |
144 | ||
145 | MODULE_DEVICE_TABLE(i2c, isl6271a_id); | |
146 | ||
147 | static struct i2c_driver isl6271a_i2c_driver = { | |
148 | .driver = { | |
149 | .name = "isl6271a", | |
259b93b2 | 150 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
51bd6943 | 151 | }, |
964e1865 | 152 | .probe = isl6271a_probe, |
51bd6943 MV |
153 | .id_table = isl6271a_id, |
154 | }; | |
155 | ||
156 | static int __init isl6271a_init(void) | |
157 | { | |
158 | return i2c_add_driver(&isl6271a_i2c_driver); | |
159 | } | |
160 | ||
161 | static void __exit isl6271a_cleanup(void) | |
162 | { | |
163 | i2c_del_driver(&isl6271a_i2c_driver); | |
164 | } | |
165 | ||
166 | subsys_initcall(isl6271a_init); | |
167 | module_exit(isl6271a_cleanup); | |
168 | ||
169 | MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>"); | |
170 | MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver"); | |
171 | MODULE_LICENSE("GPL v2"); |