hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/regulator/ltc3589.c
....@@ -1,21 +1,9 @@
1
-/*
2
- * Linear Technology LTC3589,LTC3589-1 regulator support
3
- *
4
- * Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
5
- *
6
- * See file CREDITS for list of people who contributed to this
7
- * project.
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2
11
- * as published by the Free Software Foundation.
12
- *
13
- * This program is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- * GNU General Public License for more details.
17
- *
18
- */
1
+// SPDX-License-Identifier: GPL-2.0
2
+//
3
+// Linear Technology LTC3589,LTC3589-1 regulator support
4
+//
5
+// Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
6
+
197 #include <linux/i2c.h>
208 #include <linux/init.h>
219 #include <linux/interrupt.h>
....@@ -84,19 +72,11 @@
8472 LTC3589_NUM_REGULATORS,
8573 };
8674
87
-struct ltc3589_regulator {
88
- struct regulator_desc desc;
89
-
90
- /* External feedback voltage divider */
91
- unsigned int r1;
92
- unsigned int r2;
93
-};
94
-
9575 struct ltc3589 {
9676 struct regmap *regmap;
9777 struct device *dev;
9878 enum ltc3589_variant variant;
99
- struct ltc3589_regulator regulator_descs[LTC3589_NUM_REGULATORS];
79
+ struct regulator_desc regulator_descs[LTC3589_NUM_REGULATORS];
10080 struct regulator_dev *regulators[LTC3589_NUM_REGULATORS];
10181 };
10282
....@@ -196,131 +176,90 @@
196176 .get_voltage_sel = regulator_get_voltage_sel_regmap,
197177 };
198178
179
+static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
180
+{
181
+ uint64_t tmp;
199182
200
-#define LTC3589_REG(_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit) \
201
- [LTC3589_ ## _name] = { \
202
- .desc = { \
203
- .name = #_name, \
204
- .n_voltages = (dtv_mask) + 1, \
205
- .min_uV = (go_bit) ? 362500 : 0, \
206
- .uV_step = (go_bit) ? 12500 : 0, \
207
- .ramp_delay = (go_bit) ? 1750 : 0, \
208
- .fixed_uV = (dtv_mask) ? 0 : 800000, \
209
- .ops = &ltc3589_ ## _ops ## _regulator_ops, \
210
- .type = REGULATOR_VOLTAGE, \
211
- .id = LTC3589_ ## _name, \
212
- .owner = THIS_MODULE, \
213
- .vsel_reg = (dtv1_reg), \
214
- .vsel_mask = (dtv_mask), \
215
- .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
216
- .apply_bit = (go_bit), \
217
- .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
218
- .enable_mask = (en_bit), \
219
- }, \
183
+ if (uV == 0)
184
+ return 0;
185
+
186
+ tmp = (uint64_t)uV * r1;
187
+ do_div(tmp, r2);
188
+ return uV + (unsigned int)tmp;
189
+}
190
+
191
+static int ltc3589_of_parse_cb(struct device_node *np,
192
+ const struct regulator_desc *desc,
193
+ struct regulator_config *config)
194
+{
195
+ struct ltc3589 *ltc3589 = config->driver_data;
196
+ struct regulator_desc *rdesc = &ltc3589->regulator_descs[desc->id];
197
+ u32 r[2];
198
+ int ret;
199
+
200
+ /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
201
+ if (desc->id >= LTC3589_LDO3)
202
+ return 0;
203
+
204
+ ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider", r, 2);
205
+ if (ret) {
206
+ dev_err(ltc3589->dev, "Failed to parse voltage divider: %d\n",
207
+ ret);
208
+ return ret;
220209 }
221210
222
-#define LTC3589_LINEAR_REG(_name, _dtv1) \
223
- LTC3589_REG(_name, linear, LTC3589_OVEN_ ## _name, \
211
+ if (!r[0] || !r[1])
212
+ return 0;
213
+
214
+ rdesc->min_uV = ltc3589_scale(desc->min_uV, r[0], r[1]);
215
+ rdesc->uV_step = ltc3589_scale(desc->uV_step, r[0], r[1]);
216
+ rdesc->fixed_uV = ltc3589_scale(desc->fixed_uV, r[0], r[1]);
217
+
218
+ return 0;
219
+}
220
+
221
+#define LTC3589_REG(_name, _of_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit)\
222
+ [LTC3589_ ## _name] = { \
223
+ .name = #_name, \
224
+ .of_match = of_match_ptr(#_of_name), \
225
+ .regulators_node = of_match_ptr("regulators"), \
226
+ .of_parse_cb = ltc3589_of_parse_cb, \
227
+ .n_voltages = (dtv_mask) + 1, \
228
+ .min_uV = (go_bit) ? 362500 : 0, \
229
+ .uV_step = (go_bit) ? 12500 : 0, \
230
+ .ramp_delay = (go_bit) ? 1750 : 0, \
231
+ .fixed_uV = (dtv_mask) ? 0 : 800000, \
232
+ .ops = &ltc3589_ ## _ops ## _regulator_ops, \
233
+ .type = REGULATOR_VOLTAGE, \
234
+ .id = LTC3589_ ## _name, \
235
+ .owner = THIS_MODULE, \
236
+ .vsel_reg = (dtv1_reg), \
237
+ .vsel_mask = (dtv_mask), \
238
+ .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
239
+ .apply_bit = (go_bit), \
240
+ .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
241
+ .enable_mask = (en_bit), \
242
+ }
243
+
244
+#define LTC3589_LINEAR_REG(_name, _of_name, _dtv1) \
245
+ LTC3589_REG(_name, _of_name, linear, LTC3589_OVEN_ ## _name, \
224246 LTC3589_ ## _dtv1, 0x1f, \
225247 LTC3589_VCCR_ ## _name ## _GO)
226248
227
-#define LTC3589_FIXED_REG(_name) \
228
- LTC3589_REG(_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
249
+#define LTC3589_FIXED_REG(_name, _of_name) \
250
+ LTC3589_REG(_name, _of_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
229251
230
-static struct ltc3589_regulator ltc3589_regulators[LTC3589_NUM_REGULATORS] = {
231
- LTC3589_LINEAR_REG(SW1, B1DTV1),
232
- LTC3589_LINEAR_REG(SW2, B2DTV1),
233
- LTC3589_LINEAR_REG(SW3, B3DTV1),
234
- LTC3589_FIXED_REG(BB_OUT),
235
- LTC3589_REG(LDO1, fixed_standby, 0, 0, 0, 0),
236
- LTC3589_LINEAR_REG(LDO2, L2DTV1),
237
- LTC3589_FIXED_REG(LDO3),
238
- LTC3589_REG(LDO4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2, 0x60, 0),
252
+static const struct regulator_desc ltc3589_regulators[] = {
253
+ LTC3589_LINEAR_REG(SW1, sw1, B1DTV1),
254
+ LTC3589_LINEAR_REG(SW2, sw2, B2DTV1),
255
+ LTC3589_LINEAR_REG(SW3, sw3, B3DTV1),
256
+ LTC3589_FIXED_REG(BB_OUT, bb-out),
257
+ LTC3589_REG(LDO1, ldo1, fixed_standby, 0, 0, 0, 0),
258
+ LTC3589_LINEAR_REG(LDO2, ldo2, L2DTV1),
259
+ LTC3589_FIXED_REG(LDO3, ldo3),
260
+ LTC3589_REG(LDO4, ldo4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2,
261
+ 0x60, 0),
239262 };
240
-
241
-#ifdef CONFIG_OF
242
-static struct of_regulator_match ltc3589_matches[LTC3589_NUM_REGULATORS] = {
243
- { .name = "sw1", },
244
- { .name = "sw2", },
245
- { .name = "sw3", },
246
- { .name = "bb-out", },
247
- { .name = "ldo1", }, /* standby */
248
- { .name = "ldo2", },
249
- { .name = "ldo3", },
250
- { .name = "ldo4", },
251
-};
252
-
253
-static int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
254
-{
255
- struct device *dev = ltc3589->dev;
256
- struct device_node *node;
257
- int i, ret;
258
-
259
- node = of_get_child_by_name(dev->of_node, "regulators");
260
- if (!node) {
261
- dev_err(dev, "regulators node not found\n");
262
- return -EINVAL;
263
- }
264
-
265
- ret = of_regulator_match(dev, node, ltc3589_matches,
266
- ARRAY_SIZE(ltc3589_matches));
267
- of_node_put(node);
268
- if (ret < 0) {
269
- dev_err(dev, "Error parsing regulator init data: %d\n", ret);
270
- return ret;
271
- }
272
- if (ret != LTC3589_NUM_REGULATORS) {
273
- dev_err(dev, "Only %d regulators described in device tree\n",
274
- ret);
275
- return -EINVAL;
276
- }
277
-
278
- /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
279
- for (i = 0; i < LTC3589_LDO3; i++) {
280
- struct ltc3589_regulator *desc = &ltc3589->regulator_descs[i];
281
- struct device_node *np = ltc3589_matches[i].of_node;
282
- u32 vdiv[2];
283
-
284
- ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider",
285
- vdiv, 2);
286
- if (ret) {
287
- dev_err(dev, "Failed to parse voltage divider: %d\n",
288
- ret);
289
- return ret;
290
- }
291
-
292
- desc->r1 = vdiv[0];
293
- desc->r2 = vdiv[1];
294
- }
295
-
296
- return 0;
297
-}
298
-
299
-static inline struct regulator_init_data *match_init_data(int index)
300
-{
301
- return ltc3589_matches[index].init_data;
302
-}
303
-
304
-static inline struct device_node *match_of_node(int index)
305
-{
306
- return ltc3589_matches[index].of_node;
307
-}
308
-#else
309
-static inline int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
310
-{
311
- return 0;
312
-}
313
-
314
-static inline struct regulator_init_data *match_init_data(int index)
315
-{
316
- return NULL;
317
-}
318
-
319
-static inline struct device_node *match_of_node(int index)
320
-{
321
- return NULL;
322
-}
323
-#endif
324263
325264 static bool ltc3589_writeable_reg(struct device *dev, unsigned int reg)
326265 {
....@@ -404,10 +343,10 @@
404343 .max_register = LTC3589_L2DTV2,
405344 .reg_defaults = ltc3589_reg_defaults,
406345 .num_reg_defaults = ARRAY_SIZE(ltc3589_reg_defaults),
407
- .use_single_rw = true,
346
+ .use_single_read = true,
347
+ .use_single_write = true,
408348 .cache_type = REGCACHE_RBTREE,
409349 };
410
-
411350
412351 static irqreturn_t ltc3589_isr(int irq, void *dev_id)
413352 {
....@@ -436,33 +375,11 @@
436375 return IRQ_HANDLED;
437376 }
438377
439
-static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
440
-{
441
- uint64_t tmp;
442
- if (uV == 0)
443
- return 0;
444
- tmp = (uint64_t)uV * r1;
445
- do_div(tmp, r2);
446
- return uV + (unsigned int)tmp;
447
-}
448
-
449
-static void ltc3589_apply_fb_voltage_divider(struct ltc3589_regulator *rdesc)
450
-{
451
- struct regulator_desc *desc = &rdesc->desc;
452
-
453
- if (!rdesc->r1 || !rdesc->r2)
454
- return;
455
-
456
- desc->min_uV = ltc3589_scale(desc->min_uV, rdesc->r1, rdesc->r2);
457
- desc->uV_step = ltc3589_scale(desc->uV_step, rdesc->r1, rdesc->r2);
458
- desc->fixed_uV = ltc3589_scale(desc->fixed_uV, rdesc->r1, rdesc->r2);
459
-}
460
-
461378 static int ltc3589_probe(struct i2c_client *client,
462379 const struct i2c_device_id *id)
463380 {
464381 struct device *dev = &client->dev;
465
- struct ltc3589_regulator *descs;
382
+ struct regulator_desc *descs;
466383 struct ltc3589 *ltc3589;
467384 int i, ret;
468385
....@@ -481,11 +398,11 @@
481398 descs = ltc3589->regulator_descs;
482399 memcpy(descs, ltc3589_regulators, sizeof(ltc3589_regulators));
483400 if (ltc3589->variant == LTC3589) {
484
- descs[LTC3589_LDO3].desc.fixed_uV = 1800000;
485
- descs[LTC3589_LDO4].desc.volt_table = ltc3589_ldo4;
401
+ descs[LTC3589_LDO3].fixed_uV = 1800000;
402
+ descs[LTC3589_LDO4].volt_table = ltc3589_ldo4;
486403 } else {
487
- descs[LTC3589_LDO3].desc.fixed_uV = 2800000;
488
- descs[LTC3589_LDO4].desc.volt_table = ltc3589_12_ldo4;
404
+ descs[LTC3589_LDO3].fixed_uV = 2800000;
405
+ descs[LTC3589_LDO4].volt_table = ltc3589_12_ldo4;
489406 }
490407
491408 ltc3589->regmap = devm_regmap_init_i2c(client, &ltc3589_regmap_config);
....@@ -495,25 +412,12 @@
495412 return ret;
496413 }
497414
498
- ret = ltc3589_parse_regulators_dt(ltc3589);
499
- if (ret)
500
- return ret;
501
-
502415 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
503
- struct ltc3589_regulator *rdesc = &ltc3589->regulator_descs[i];
504
- struct regulator_desc *desc = &rdesc->desc;
505
- struct regulator_init_data *init_data;
416
+ struct regulator_desc *desc = &ltc3589->regulator_descs[i];
506417 struct regulator_config config = { };
507418
508
- init_data = match_init_data(i);
509
-
510
- if (i < LTC3589_LDO3)
511
- ltc3589_apply_fb_voltage_divider(rdesc);
512
-
513419 config.dev = dev;
514
- config.init_data = init_data;
515420 config.driver_data = ltc3589;
516
- config.of_node = match_of_node(i);
517421
518422 ltc3589->regulators[i] = devm_regulator_register(dev, desc,
519423 &config);
....@@ -547,7 +451,7 @@
547451 };
548452 MODULE_DEVICE_TABLE(i2c, ltc3589_i2c_id);
549453
550
-static const struct of_device_id ltc3589_of_match[] = {
454
+static const struct of_device_id __maybe_unused ltc3589_of_match[] = {
551455 {
552456 .compatible = "lltc,ltc3589",
553457 .data = (void *)LTC3589,