forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/regulator/gpio-regulator.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * gpio-regulator.c
34 *
....@@ -12,11 +13,6 @@
1213 * Copyright (c) 2009 Nokia Corporation
1314 * Roger Quadros <ext-roger.quadros@nokia.com>
1415 *
15
- * This program is free software; you can redistribute it and/or
16
- * modify it under the terms of the GNU General Public License as
17
- * published by the Free Software Foundation; either version 2 of the
18
- * License, or (at your option) any later version.
19
- *
2016 * This is useful for systems with mixed controllable and
2117 * non-controllable regulators, as well as for allowing testing on
2218 * systems with no controllable regulators.
....@@ -30,16 +26,14 @@
3026 #include <linux/regulator/machine.h>
3127 #include <linux/regulator/of_regulator.h>
3228 #include <linux/regulator/gpio-regulator.h>
33
-#include <linux/gpio.h>
29
+#include <linux/gpio/consumer.h>
3430 #include <linux/slab.h>
3531 #include <linux/of.h>
36
-#include <linux/of_gpio.h>
3732
3833 struct gpio_regulator_data {
3934 struct regulator_desc desc;
40
- struct regulator_dev *dev;
4135
42
- struct gpio *gpios;
36
+ struct gpio_desc **gpiods;
4337 int nr_gpios;
4438
4539 struct gpio_regulator_state *states;
....@@ -82,7 +76,7 @@
8276
8377 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
8478 state = (target & (1 << ptr)) >> ptr;
85
- gpio_set_value_cansleep(data->gpios[ptr].gpio, state);
79
+ gpiod_set_value_cansleep(data->gpiods[ptr], state);
8680 }
8781 data->state = target;
8882
....@@ -119,14 +113,14 @@
119113
120114 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
121115 state = (target & (1 << ptr)) >> ptr;
122
- gpio_set_value_cansleep(data->gpios[ptr].gpio, state);
116
+ gpiod_set_value_cansleep(data->gpiods[ptr], state);
123117 }
124118 data->state = target;
125119
126120 return 0;
127121 }
128122
129
-static struct regulator_ops gpio_regulator_voltage_ops = {
123
+static const struct regulator_ops gpio_regulator_voltage_ops = {
130124 .get_voltage = gpio_regulator_get_value,
131125 .set_voltage = gpio_regulator_set_voltage,
132126 .list_voltage = gpio_regulator_list_voltage,
....@@ -138,7 +132,8 @@
138132 {
139133 struct gpio_regulator_config *config;
140134 const char *regtype;
141
- int proplen, gpio, i;
135
+ int proplen, i;
136
+ int ngpios;
142137 int ret;
143138
144139 config = devm_kzalloc(dev,
....@@ -153,59 +148,43 @@
153148
154149 config->supply_name = config->init_data->constraints.name;
155150
156
- if (of_property_read_bool(np, "enable-active-high"))
157
- config->enable_high = true;
151
+ if (config->init_data->constraints.boot_on)
152
+ config->enabled_at_boot = true;
158153
154
+ /*
155
+ * Do not use: undocumented device tree property.
156
+ * This is kept around solely for device tree ABI stability.
157
+ */
159158 if (of_property_read_bool(np, "enable-at-boot"))
160159 config->enabled_at_boot = true;
161160
162161 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
163162
164
- config->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
165
- if (config->enable_gpio < 0 && config->enable_gpio != -ENOENT)
166
- return ERR_PTR(config->enable_gpio);
167
-
168
- /* Fetch GPIOs. - optional property*/
169
- ret = of_gpio_count(np);
170
- if ((ret < 0) && (ret != -ENOENT))
171
- return ERR_PTR(ret);
172
-
173
- if (ret > 0) {
174
- config->nr_gpios = ret;
175
- config->gpios = devm_kcalloc(dev,
176
- config->nr_gpios, sizeof(struct gpio),
177
- GFP_KERNEL);
178
- if (!config->gpios)
163
+ /* Fetch GPIO init levels */
164
+ ngpios = gpiod_count(dev, NULL);
165
+ if (ngpios > 0) {
166
+ config->gflags = devm_kzalloc(dev,
167
+ sizeof(enum gpiod_flags)
168
+ * ngpios,
169
+ GFP_KERNEL);
170
+ if (!config->gflags)
179171 return ERR_PTR(-ENOMEM);
180172
181
- proplen = of_property_count_u32_elems(np, "gpios-states");
182
- /* optional property */
183
- if (proplen < 0)
184
- proplen = 0;
173
+ for (i = 0; i < ngpios; i++) {
174
+ u32 val;
185175
186
- if (proplen > 0 && proplen != config->nr_gpios) {
187
- dev_warn(dev, "gpios <-> gpios-states mismatch\n");
188
- proplen = 0;
189
- }
176
+ ret = of_property_read_u32_index(np, "gpios-states", i,
177
+ &val);
190178
191
- for (i = 0; i < config->nr_gpios; i++) {
192
- gpio = of_get_named_gpio(np, "gpios", i);
193
- if (gpio < 0) {
194
- if (gpio != -ENOENT)
195
- return ERR_PTR(gpio);
196
- break;
197
- }
198
- config->gpios[i].gpio = gpio;
199
- config->gpios[i].label = config->supply_name;
200
- if (proplen > 0) {
201
- of_property_read_u32_index(np, "gpios-states",
202
- i, &ret);
203
- if (ret)
204
- config->gpios[i].flags =
205
- GPIOF_OUT_INIT_HIGH;
206
- }
179
+ /* Default to high per specification */
180
+ if (ret)
181
+ config->gflags[i] = GPIOD_OUT_HIGH;
182
+ else
183
+ config->gflags[i] =
184
+ val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
207185 }
208186 }
187
+ config->ngpios = ngpios;
209188
210189 /* Fetch states. */
211190 proplen = of_property_count_u32_elems(np, "states");
....@@ -241,84 +220,67 @@
241220 regtype);
242221 }
243222
244
- if (of_find_property(np, "vin-supply", NULL))
245
- config->input_supply = "vin";
246
-
247223 return config;
248224 }
249225
250
-static struct regulator_ops gpio_regulator_current_ops = {
226
+static const struct regulator_ops gpio_regulator_current_ops = {
251227 .get_current_limit = gpio_regulator_get_value,
252228 .set_current_limit = gpio_regulator_set_current_limit,
253229 };
254230
255231 static int gpio_regulator_probe(struct platform_device *pdev)
256232 {
257
- struct gpio_regulator_config *config = dev_get_platdata(&pdev->dev);
258
- struct device_node *np = pdev->dev.of_node;
233
+ struct device *dev = &pdev->dev;
234
+ struct gpio_regulator_config *config = dev_get_platdata(dev);
235
+ struct device_node *np = dev->of_node;
259236 struct gpio_regulator_data *drvdata;
260237 struct regulator_config cfg = { };
261
- int ptr, ret, state;
238
+ struct regulator_dev *rdev;
239
+ enum gpiod_flags gflags;
240
+ int ptr, ret, state, i;
262241
263
- drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
242
+ drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
264243 GFP_KERNEL);
265244 if (drvdata == NULL)
266245 return -ENOMEM;
267246
268247 if (np) {
269
- config = of_get_gpio_regulator_config(&pdev->dev, np,
248
+ config = of_get_gpio_regulator_config(dev, np,
270249 &drvdata->desc);
271250 if (IS_ERR(config))
272251 return PTR_ERR(config);
273252 }
274253
275
- drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
254
+ drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
276255 if (drvdata->desc.name == NULL) {
277
- dev_err(&pdev->dev, "Failed to allocate supply name\n");
256
+ dev_err(dev, "Failed to allocate supply name\n");
278257 return -ENOMEM;
279258 }
280259
281
- if (config->input_supply) {
282
- drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
283
- config->input_supply,
284
- GFP_KERNEL);
285
- if (!drvdata->desc.supply_name) {
286
- dev_err(&pdev->dev,
287
- "Failed to allocate input supply\n");
288
- ret = -ENOMEM;
289
- goto err_name;
290
- }
260
+ drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
261
+ GFP_KERNEL);
262
+ if (!drvdata->gpiods)
263
+ return -ENOMEM;
264
+ for (i = 0; i < config->ngpios; i++) {
265
+ drvdata->gpiods[i] = devm_gpiod_get_index(dev,
266
+ NULL,
267
+ i,
268
+ config->gflags[i]);
269
+ if (IS_ERR(drvdata->gpiods[i]))
270
+ return PTR_ERR(drvdata->gpiods[i]);
271
+ /* This is good to know */
272
+ gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
291273 }
274
+ drvdata->nr_gpios = config->ngpios;
292275
293
- if (config->nr_gpios != 0) {
294
- drvdata->gpios = kmemdup(config->gpios,
295
- config->nr_gpios * sizeof(struct gpio),
296
- GFP_KERNEL);
297
- if (drvdata->gpios == NULL) {
298
- dev_err(&pdev->dev, "Failed to allocate gpio data\n");
299
- ret = -ENOMEM;
300
- goto err_name;
301
- }
302
-
303
- drvdata->nr_gpios = config->nr_gpios;
304
- ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
305
- if (ret) {
306
- if (ret != -EPROBE_DEFER)
307
- dev_err(&pdev->dev,
308
- "Could not obtain regulator setting GPIOs: %d\n",
309
- ret);
310
- goto err_memgpio;
311
- }
312
- }
313
-
314
- drvdata->states = kmemdup(config->states,
315
- config->nr_states *
316
- sizeof(struct gpio_regulator_state),
317
- GFP_KERNEL);
276
+ drvdata->states = devm_kmemdup(dev,
277
+ config->states,
278
+ config->nr_states *
279
+ sizeof(struct gpio_regulator_state),
280
+ GFP_KERNEL);
318281 if (drvdata->states == NULL) {
319
- dev_err(&pdev->dev, "Failed to allocate state data\n");
320
- ret = -ENOMEM;
321
- goto err_stategpio;
282
+ dev_err(dev, "Failed to allocate state data\n");
283
+ return -ENOMEM;
322284 }
323285 drvdata->nr_states = config->nr_states;
324286
....@@ -337,75 +299,44 @@
337299 drvdata->desc.ops = &gpio_regulator_current_ops;
338300 break;
339301 default:
340
- dev_err(&pdev->dev, "No regulator type set\n");
341
- ret = -EINVAL;
342
- goto err_memstate;
302
+ dev_err(dev, "No regulator type set\n");
303
+ return -EINVAL;
343304 }
344305
345306 /* build initial state from gpio init data. */
346307 state = 0;
347308 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
348
- if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
309
+ if (config->gflags[ptr] == GPIOD_OUT_HIGH)
349310 state |= (1 << ptr);
350311 }
351312 drvdata->state = state;
352313
353
- cfg.dev = &pdev->dev;
314
+ cfg.dev = dev;
354315 cfg.init_data = config->init_data;
355316 cfg.driver_data = drvdata;
356317 cfg.of_node = np;
357318
358
- if (gpio_is_valid(config->enable_gpio)) {
359
- cfg.ena_gpio = config->enable_gpio;
360
- cfg.ena_gpio_initialized = true;
361
- }
362
- cfg.ena_gpio_invert = !config->enable_high;
363
- if (config->enabled_at_boot) {
364
- if (config->enable_high)
365
- cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
366
- else
367
- cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
368
- } else {
369
- if (config->enable_high)
370
- cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
371
- else
372
- cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
373
- }
319
+ /*
320
+ * The signal will be inverted by the GPIO core if flagged so in the
321
+ * descriptor.
322
+ */
323
+ if (config->enabled_at_boot)
324
+ gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
325
+ else
326
+ gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
374327
375
- drvdata->dev = regulator_register(&drvdata->desc, &cfg);
376
- if (IS_ERR(drvdata->dev)) {
377
- ret = PTR_ERR(drvdata->dev);
378
- dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
379
- goto err_memstate;
328
+ cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
329
+ if (IS_ERR(cfg.ena_gpiod))
330
+ return PTR_ERR(cfg.ena_gpiod);
331
+
332
+ rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
333
+ if (IS_ERR(rdev)) {
334
+ ret = PTR_ERR(rdev);
335
+ dev_err(dev, "Failed to register regulator: %d\n", ret);
336
+ return ret;
380337 }
381338
382339 platform_set_drvdata(pdev, drvdata);
383
-
384
- return 0;
385
-
386
-err_memstate:
387
- kfree(drvdata->states);
388
-err_stategpio:
389
- gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
390
-err_memgpio:
391
- kfree(drvdata->gpios);
392
-err_name:
393
- kfree(drvdata->desc.name);
394
- return ret;
395
-}
396
-
397
-static int gpio_regulator_remove(struct platform_device *pdev)
398
-{
399
- struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
400
-
401
- regulator_unregister(drvdata->dev);
402
-
403
- gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
404
-
405
- kfree(drvdata->states);
406
- kfree(drvdata->gpios);
407
-
408
- kfree(drvdata->desc.name);
409340
410341 return 0;
411342 }
....@@ -420,7 +351,6 @@
420351
421352 static struct platform_driver gpio_regulator_driver = {
422353 .probe = gpio_regulator_probe,
423
- .remove = gpio_regulator_remove,
424354 .driver = {
425355 .name = "gpio-regulator",
426356 .of_match_table = of_match_ptr(regulator_gpio_of_match),