blob: a4456db5849d06282b7eb2884d20e8da29093753 [file] [log] [blame]
David Brownellfa16a5c2009-02-08 10:37:06 -08001/*
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01002 * twl-regulator.c -- support regulators in twl4030/twl6030 family chips
David Brownellfa16a5c2009-02-08 10:37:06 -08003 *
4 * Copyright (C) 2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/module.h>
Stephen Rothwell8f52a582012-08-16 15:16:05 +100013#include <linux/string.h>
14#include <linux/slab.h>
David Brownellfa16a5c2009-02-08 10:37:06 -080015#include <linux/init.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
Rajendra Nayak2098e952012-02-28 15:09:11 +053018#include <linux/of.h>
19#include <linux/of_device.h>
David Brownellfa16a5c2009-02-08 10:37:06 -080020#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
Rajendra Nayak2098e952012-02-28 15:09:11 +053022#include <linux/regulator/of_regulator.h>
Wolfram Sanga2054252017-08-14 18:34:24 +020023#include <linux/mfd/twl.h>
Ivaylo Dimitrov2330b052016-03-26 10:28:13 +020024#include <linux/delay.h>
David Brownellfa16a5c2009-02-08 10:37:06 -080025
26/*
Nicolae Rosiacac28ae2016-11-12 14:42:15 +020027 * The TWL4030/TW5030/TPS659x0 family chips include power management, a
David Brownellfa16a5c2009-02-08 10:37:06 -080028 * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
29 * include an audio codec, battery charger, and more voltage regulators.
30 * These chips are often used in OMAP-based systems.
31 *
32 * This driver implements software-based resource control for various
33 * voltage regulators. This is usually augmented with state machine
34 * based control.
35 */
36
37struct twlreg_info {
38 /* start of regulator's PM_RECEIVER control register bank */
39 u8 base;
40
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +010041 /* twl resource ID, for resource control state machine */
David Brownellfa16a5c2009-02-08 10:37:06 -080042 u8 id;
43
44 /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
45 u8 table_len;
46 const u16 *table;
47
Juha Keski-Saari045f9722009-12-16 14:49:52 +020048 /* State REMAP default configuration */
49 u8 remap;
50
David Brownellfa16a5c2009-02-08 10:37:06 -080051 /* used by regulator core */
52 struct regulator_desc desc;
Graeme Gregory4d94aee2011-05-22 21:21:23 +010053
54 /* chip specific features */
Jingoo Han3db39882014-01-08 10:04:48 +090055 unsigned long features;
Tero Kristo63bfff42012-02-16 12:27:52 +020056
Tero Kristo63bfff42012-02-16 12:27:52 +020057 /* data passed from board for external get/set voltage */
58 void *data;
David Brownellfa16a5c2009-02-08 10:37:06 -080059};
60
61
62/* LDO control registers ... offset is from the base of its register bank.
63 * The first three registers of all power resource banks help hardware to
64 * manage the various resource groups.
65 */
Rajendra Nayak441a4502009-12-13 22:19:23 +010066/* Common offset in TWL4030/6030 */
David Brownellfa16a5c2009-02-08 10:37:06 -080067#define VREG_GRP 0
Rajendra Nayak441a4502009-12-13 22:19:23 +010068/* TWL4030 register offsets */
David Brownellfa16a5c2009-02-08 10:37:06 -080069#define VREG_TYPE 1
70#define VREG_REMAP 2
71#define VREG_DEDICATED 3 /* LDO control */
Tero Kristoba305e32011-11-28 16:53:19 +020072#define VREG_VOLTAGE_SMPS_4030 9
Rajendra Nayak441a4502009-12-13 22:19:23 +010073/* TWL6030 register offsets */
74#define VREG_TRANS 1
75#define VREG_STATE 2
76#define VREG_VOLTAGE 3
Graeme Gregory4d94aee2011-05-22 21:21:23 +010077#define VREG_VOLTAGE_SMPS 4
Graeme Gregory4d94aee2011-05-22 21:21:23 +010078
David Brownellfa16a5c2009-02-08 10:37:06 -080079static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010080twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
David Brownellfa16a5c2009-02-08 10:37:06 -080081{
82 u8 value;
83 int status;
84
Rajendra Nayak441a4502009-12-13 22:19:23 +010085 status = twl_i2c_read_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080086 &value, info->base + offset);
87 return (status < 0) ? status : value;
88}
89
90static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010091twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
92 u8 value)
David Brownellfa16a5c2009-02-08 10:37:06 -080093{
Rajendra Nayak441a4502009-12-13 22:19:23 +010094 return twl_i2c_write_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080095 value, info->base + offset);
96}
97
98/*----------------------------------------------------------------------*/
99
100/* generic power resource operations, which work on all regulators */
101
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100102static int twlreg_grp(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800103{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100104 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
105 VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800106}
107
108/*
109 * Enable/disable regulators by joining/leaving the P1 (processor) group.
110 * We assume nobody else is updating the DEV_GRP registers.
111 */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100112/* definition for 4030 family */
113#define P3_GRP_4030 BIT(7) /* "peripherals" */
114#define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
115#define P1_GRP_4030 BIT(5) /* CPU/Linux */
116/* definition for 6030 family */
117#define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
118#define P2_GRP_6030 BIT(1) /* "peripherals" */
119#define P1_GRP_6030 BIT(0) /* CPU/Linux */
David Brownellfa16a5c2009-02-08 10:37:06 -0800120
Saquib Hermanb2456772011-04-01 10:22:44 +0530121static int twl4030reg_is_enabled(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800122{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100123 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800124
125 if (state < 0)
126 return state;
127
Saquib Hermanb2456772011-04-01 10:22:44 +0530128 return state & P1_GRP_4030;
129}
130
Ivaylo Dimitrov2330b052016-03-26 10:28:13 +0200131#define PB_I2C_BUSY BIT(0)
132#define PB_I2C_BWEN BIT(1)
133
134/* Wait until buffer empty/ready to send a word on power bus. */
135static int twl4030_wait_pb_ready(void)
136{
137
138 int ret;
139 int timeout = 10;
140 u8 val;
141
142 do {
143 ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
144 TWL4030_PM_MASTER_PB_CFG);
145 if (ret < 0)
146 return ret;
147
148 if (!(val & PB_I2C_BUSY))
149 return 0;
150
151 mdelay(1);
152 timeout--;
153 } while (timeout);
154
155 return -ETIMEDOUT;
156}
157
158/* Send a word over the powerbus */
159static int twl4030_send_pb_msg(unsigned msg)
160{
161 u8 val;
162 int ret;
163
164 /* save powerbus configuration */
165 ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
166 TWL4030_PM_MASTER_PB_CFG);
167 if (ret < 0)
168 return ret;
169
170 /* Enable i2c access to powerbus */
171 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN,
172 TWL4030_PM_MASTER_PB_CFG);
173 if (ret < 0)
174 return ret;
175
176 ret = twl4030_wait_pb_ready();
177 if (ret < 0)
178 return ret;
179
180 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8,
181 TWL4030_PM_MASTER_PB_WORD_MSB);
182 if (ret < 0)
183 return ret;
184
185 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff,
186 TWL4030_PM_MASTER_PB_WORD_LSB);
187 if (ret < 0)
188 return ret;
189
190 ret = twl4030_wait_pb_ready();
191 if (ret < 0)
192 return ret;
193
194 /* Restore powerbus configuration */
195 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
Ivaylo Dimitrov74d8b452016-04-06 09:06:03 +0300196 TWL4030_PM_MASTER_PB_CFG);
Ivaylo Dimitrov2330b052016-03-26 10:28:13 +0200197}
198
Balaji T Kf8c29402011-05-20 19:03:51 +0530199static int twl4030reg_enable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800200{
201 struct twlreg_info *info = rdev_get_drvdata(rdev);
202 int grp;
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200203 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800204
Axel Linb6f476c2012-04-11 11:07:17 +0800205 grp = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800206 if (grp < 0)
207 return grp;
208
Balaji T Kf8c29402011-05-20 19:03:51 +0530209 grp |= P1_GRP_4030;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100210
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200211 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
212
Balaji T Kf8c29402011-05-20 19:03:51 +0530213 return ret;
214}
215
Balaji T K0ff38972011-05-20 19:03:52 +0530216static int twl4030reg_disable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800217{
218 struct twlreg_info *info = rdev_get_drvdata(rdev);
219 int grp;
Saquib Herman21657ebf2011-04-01 10:22:42 +0530220 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800221
Axel Linb6f476c2012-04-11 11:07:17 +0800222 grp = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800223 if (grp < 0)
224 return grp;
225
Balaji T K0ff38972011-05-20 19:03:52 +0530226 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100227
Saquib Herman21657ebf2011-04-01 10:22:42 +0530228 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
229
Balaji T K0ff38972011-05-20 19:03:52 +0530230 return ret;
231}
232
Saquib Herman9a0244a2011-04-01 10:22:45 +0530233static int twl4030reg_get_status(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800234{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100235 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800236
237 if (state < 0)
238 return state;
239 state &= 0x0f;
240
241 /* assume state != WARM_RESET; we'd not be running... */
242 if (!state)
243 return REGULATOR_STATUS_OFF;
244 return (state & BIT(3))
245 ? REGULATOR_STATUS_NORMAL
246 : REGULATOR_STATUS_STANDBY;
247}
248
Saquib Herman1a399622011-04-01 10:22:46 +0530249static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
David Brownellfa16a5c2009-02-08 10:37:06 -0800250{
251 struct twlreg_info *info = rdev_get_drvdata(rdev);
252 unsigned message;
David Brownellfa16a5c2009-02-08 10:37:06 -0800253
254 /* We can only set the mode through state machine commands... */
255 switch (mode) {
256 case REGULATOR_MODE_NORMAL:
257 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
258 break;
259 case REGULATOR_MODE_STANDBY:
260 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
261 break;
262 default:
263 return -EINVAL;
264 }
265
Ivaylo Dimitrov2330b052016-03-26 10:28:13 +0200266 return twl4030_send_pb_msg(message);
David Brownellfa16a5c2009-02-08 10:37:06 -0800267}
268
Ivaylo Dimitrova221f952016-04-05 08:59:34 +0300269static inline unsigned int twl4030reg_map_mode(unsigned int mode)
270{
271 switch (mode) {
272 case RES_STATE_ACTIVE:
273 return REGULATOR_MODE_NORMAL;
274 case RES_STATE_SLEEP:
275 return REGULATOR_MODE_STANDBY;
276 default:
277 return -EINVAL;
278 }
279}
280
David Brownellfa16a5c2009-02-08 10:37:06 -0800281/*----------------------------------------------------------------------*/
282
283/*
284 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
285 * select field in its control register. We use tables indexed by VSEL
286 * to record voltages in milliVolts. (Accuracy is about three percent.)
287 *
288 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
289 * currently handled by listing two slightly different VAUX2 regulators,
290 * only one of which will be configured.
291 *
292 * VSEL values documented as "TI cannot support these values" are flagged
293 * in these tables as UNSUP() values; we normally won't assign them.
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200294 *
295 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
296 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
David Brownellfa16a5c2009-02-08 10:37:06 -0800297 */
David Brownellfa16a5c2009-02-08 10:37:06 -0800298#define UNSUP_MASK 0x8000
David Brownellfa16a5c2009-02-08 10:37:06 -0800299
300#define UNSUP(x) (UNSUP_MASK | (x))
NeilBrown411a2df2012-05-09 05:44:00 +1000301#define IS_UNSUP(info, x) \
302 ((UNSUP_MASK & (x)) && \
303 !((info)->features & TWL4030_ALLOW_UNSUPPORTED))
David Brownellfa16a5c2009-02-08 10:37:06 -0800304#define LDO_MV(x) (~UNSUP_MASK & (x))
305
306
307static const u16 VAUX1_VSEL_table[] = {
308 UNSUP(1500), UNSUP(1800), 2500, 2800,
309 3000, 3000, 3000, 3000,
310};
311static const u16 VAUX2_4030_VSEL_table[] = {
312 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
313 1500, 1800, UNSUP(1850), 2500,
314 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
315 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
316};
317static const u16 VAUX2_VSEL_table[] = {
318 1700, 1700, 1900, 1300,
319 1500, 1800, 2000, 2500,
320 2100, 2800, 2200, 2300,
321 2400, 2400, 2400, 2400,
322};
323static const u16 VAUX3_VSEL_table[] = {
324 1500, 1800, 2500, 2800,
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200325 3000, 3000, 3000, 3000,
David Brownellfa16a5c2009-02-08 10:37:06 -0800326};
327static const u16 VAUX4_VSEL_table[] = {
328 700, 1000, 1200, UNSUP(1300),
329 1500, 1800, UNSUP(1850), 2500,
David Brownell1897e742009-03-10 11:51:15 -0800330 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
331 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
David Brownellfa16a5c2009-02-08 10:37:06 -0800332};
333static const u16 VMMC1_VSEL_table[] = {
334 1850, 2850, 3000, 3150,
335};
336static const u16 VMMC2_VSEL_table[] = {
337 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
338 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
339 2600, 2800, 2850, 3000,
340 3150, 3150, 3150, 3150,
341};
342static const u16 VPLL1_VSEL_table[] = {
343 1000, 1200, 1300, 1800,
344 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
345};
346static const u16 VPLL2_VSEL_table[] = {
347 700, 1000, 1200, 1300,
348 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
349 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
350 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
351};
352static const u16 VSIM_VSEL_table[] = {
353 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
354 2800, 3000, 3000, 3000,
355};
356static const u16 VDAC_VSEL_table[] = {
357 1200, 1300, 1800, 1800,
358};
Juha Keski-Saari07fc4932009-12-16 15:27:55 +0200359static const u16 VIO_VSEL_table[] = {
360 1800, 1850,
361};
362static const u16 VINTANA2_VSEL_table[] = {
363 2500, 2750,
364};
David Brownellfa16a5c2009-02-08 10:37:06 -0800365
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530366static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800367{
368 struct twlreg_info *info = rdev_get_drvdata(rdev);
369 int mV = info->table[index];
370
NeilBrown411a2df2012-05-09 05:44:00 +1000371 return IS_UNSUP(info, mV) ? 0 : (LDO_MV(mV) * 1000);
David Brownell66b659e2009-02-26 11:50:14 -0800372}
373
David Brownellfa16a5c2009-02-08 10:37:06 -0800374static int
Axel Lindd16b1f2012-03-26 09:30:06 +0800375twl4030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
David Brownellfa16a5c2009-02-08 10:37:06 -0800376{
377 struct twlreg_info *info = rdev_get_drvdata(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800378
Axel Lindd16b1f2012-03-26 09:30:06 +0800379 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
380 selector);
David Brownellfa16a5c2009-02-08 10:37:06 -0800381}
382
Axel Lin6949fbe2013-02-16 10:09:54 +0800383static int twl4030ldo_get_voltage_sel(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800384{
385 struct twlreg_info *info = rdev_get_drvdata(rdev);
Axel Lin6949fbe2013-02-16 10:09:54 +0800386 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
David Brownellfa16a5c2009-02-08 10:37:06 -0800387
388 if (vsel < 0)
389 return vsel;
390
391 vsel &= info->table_len - 1;
Axel Lin6949fbe2013-02-16 10:09:54 +0800392 return vsel;
David Brownellfa16a5c2009-02-08 10:37:06 -0800393}
394
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530395static struct regulator_ops twl4030ldo_ops = {
396 .list_voltage = twl4030ldo_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800397
Axel Lindd16b1f2012-03-26 09:30:06 +0800398 .set_voltage_sel = twl4030ldo_set_voltage_sel,
Axel Lin6949fbe2013-02-16 10:09:54 +0800399 .get_voltage_sel = twl4030ldo_get_voltage_sel,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530400
Balaji T Kf8c29402011-05-20 19:03:51 +0530401 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530402 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530403 .is_enabled = twl4030reg_is_enabled,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530404
Saquib Herman1a399622011-04-01 10:22:46 +0530405 .set_mode = twl4030reg_set_mode,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530406
Saquib Herman9a0244a2011-04-01 10:22:45 +0530407 .get_status = twl4030reg_get_status,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530408};
409
Tero Kristoba305e32011-11-28 16:53:19 +0200410static int
411twl4030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
412 unsigned *selector)
413{
414 struct twlreg_info *info = rdev_get_drvdata(rdev);
415 int vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
416
Nicolae Rosia8313a4f2016-11-12 14:42:13 +0200417 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS_4030, vsel);
Tero Kristo63bfff42012-02-16 12:27:52 +0200418
Tero Kristoba305e32011-11-28 16:53:19 +0200419 return 0;
420}
421
422static int twl4030smps_get_voltage(struct regulator_dev *rdev)
423{
424 struct twlreg_info *info = rdev_get_drvdata(rdev);
Tero Kristo63bfff42012-02-16 12:27:52 +0200425 int vsel;
426
Tero Kristo63bfff42012-02-16 12:27:52 +0200427 vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
Tero Kristoba305e32011-11-28 16:53:19 +0200428 VREG_VOLTAGE_SMPS_4030);
429
430 return vsel * 12500 + 600000;
431}
432
433static struct regulator_ops twl4030smps_ops = {
434 .set_voltage = twl4030smps_set_voltage,
435 .get_voltage = twl4030smps_get_voltage,
436};
437
David Brownellfa16a5c2009-02-08 10:37:06 -0800438/*----------------------------------------------------------------------*/
439
Saquib Hermanb2456772011-04-01 10:22:44 +0530440static struct regulator_ops twl4030fixed_ops = {
Axel Linb3816d52012-12-22 13:31:19 +0800441 .list_voltage = regulator_list_voltage_linear,
David Brownell66b659e2009-02-26 11:50:14 -0800442
Balaji T Kf8c29402011-05-20 19:03:51 +0530443 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530444 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530445 .is_enabled = twl4030reg_is_enabled,
446
Saquib Herman1a399622011-04-01 10:22:46 +0530447 .set_mode = twl4030reg_set_mode,
Saquib Hermanb2456772011-04-01 10:22:44 +0530448
Saquib Herman9a0244a2011-04-01 10:22:45 +0530449 .get_status = twl4030reg_get_status,
Saquib Hermanb2456772011-04-01 10:22:44 +0530450};
451
David Brownellfa16a5c2009-02-08 10:37:06 -0800452/*----------------------------------------------------------------------*/
453
Rajendra Nayak2098e952012-02-28 15:09:11 +0530454#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) \
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000455static const struct twlreg_info TWL4030_INFO_##label = { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800456 .base = offset, \
457 .id = num, \
458 .table_len = ARRAY_SIZE(label##_VSEL_table), \
459 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200460 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800461 .desc = { \
462 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530463 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800464 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530465 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800466 .type = REGULATOR_VOLTAGE, \
467 .owner = THIS_MODULE, \
Axel Linfca53d82012-07-04 10:46:34 +0800468 .enable_time = turnon_delay, \
Ivaylo Dimitrova221f952016-04-05 08:59:34 +0300469 .of_map_mode = twl4030reg_map_mode, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800470 }, \
471 }
472
Tero Kristoba305e32011-11-28 16:53:19 +0200473#define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000474static const struct twlreg_info TWL4030_INFO_##label = { \
Tero Kristoba305e32011-11-28 16:53:19 +0200475 .base = offset, \
476 .id = num, \
Tero Kristoba305e32011-11-28 16:53:19 +0200477 .remap = remap_conf, \
478 .desc = { \
479 .name = #label, \
480 .id = TWL4030_REG_##label, \
481 .ops = &twl4030smps_ops, \
482 .type = REGULATOR_VOLTAGE, \
483 .owner = THIS_MODULE, \
Axel Linfca53d82012-07-04 10:46:34 +0800484 .enable_time = turnon_delay, \
Ivaylo Dimitrova221f952016-04-05 08:59:34 +0300485 .of_map_mode = twl4030reg_map_mode, \
Tero Kristoba305e32011-11-28 16:53:19 +0200486 }, \
487 }
488
Nicolae Rosiadab780a2016-11-12 14:42:14 +0200489#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
490 remap_conf) \
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000491static const struct twlreg_info TWLFIXED_INFO_##label = { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800492 .base = offset, \
493 .id = num, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200494 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800495 .desc = { \
496 .name = #label, \
Nicolae Rosiadab780a2016-11-12 14:42:14 +0200497 .id = TWL4030##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800498 .n_voltages = 1, \
Nicolae Rosiadab780a2016-11-12 14:42:14 +0200499 .ops = &twl4030fixed_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800500 .type = REGULATOR_VOLTAGE, \
501 .owner = THIS_MODULE, \
Axel Linb3816d52012-12-22 13:31:19 +0800502 .min_uV = mVolts * 1000, \
Axel Linfca53d82012-07-04 10:46:34 +0800503 .enable_time = turnon_delay, \
Nicolae Rosiadab780a2016-11-12 14:42:14 +0200504 .of_map_mode = twl4030reg_map_mode, \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530505 }, \
506 }
507
David Brownellfa16a5c2009-02-08 10:37:06 -0800508/*
509 * We list regulators here if systems need some level of
510 * software control over them after boot.
511 */
Rajendra Nayak2098e952012-02-28 15:09:11 +0530512TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08);
513TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08);
514TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08);
515TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08);
516TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08);
517TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08);
518TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08);
519TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00);
520TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08);
521TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00);
522TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08);
523TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08);
524TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08);
525TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08);
526TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08);
527/* VUSBCP is managed *only* by the USB subchip */
Aaro Koskinen908d6d52012-08-15 01:10:04 +0300528TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08);
Rajendra Nayak2098e952012-02-28 15:09:11 +0530529TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08);
530TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08);
531TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08);
532TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08);
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100533
Rajendra Nayak2098e952012-02-28 15:09:11 +0530534#define TWL_OF_MATCH(comp, family, label) \
535 { \
536 .compatible = comp, \
537 .data = &family##_INFO_##label, \
538 }
539
540#define TWL4030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL4030, label)
541#define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
Graeme Gregory89ce43f2013-06-19 15:24:02 +0300542#define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
Rajendra Nayak2098e952012-02-28 15:09:11 +0530543#define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
Rajendra Nayak2098e952012-02-28 15:09:11 +0530544#define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
545
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800546static const struct of_device_id twl_of_match[] = {
Rajendra Nayak2098e952012-02-28 15:09:11 +0530547 TWL4030_OF_MATCH("ti,twl4030-vaux1", VAUX1),
548 TWL4030_OF_MATCH("ti,twl4030-vaux2", VAUX2_4030),
549 TWL4030_OF_MATCH("ti,twl5030-vaux2", VAUX2),
550 TWL4030_OF_MATCH("ti,twl4030-vaux3", VAUX3),
551 TWL4030_OF_MATCH("ti,twl4030-vaux4", VAUX4),
552 TWL4030_OF_MATCH("ti,twl4030-vmmc1", VMMC1),
553 TWL4030_OF_MATCH("ti,twl4030-vmmc2", VMMC2),
554 TWL4030_OF_MATCH("ti,twl4030-vpll1", VPLL1),
555 TWL4030_OF_MATCH("ti,twl4030-vpll2", VPLL2),
556 TWL4030_OF_MATCH("ti,twl4030-vsim", VSIM),
557 TWL4030_OF_MATCH("ti,twl4030-vdac", VDAC),
558 TWL4030_OF_MATCH("ti,twl4030-vintana2", VINTANA2),
559 TWL4030_OF_MATCH("ti,twl4030-vio", VIO),
560 TWL4030_OF_MATCH("ti,twl4030-vdd1", VDD1),
561 TWL4030_OF_MATCH("ti,twl4030-vdd2", VDD2),
Aaro Koskinen908d6d52012-08-15 01:10:04 +0300562 TWLFIXED_OF_MATCH("ti,twl4030-vintana1", VINTANA1),
Rajendra Nayak2098e952012-02-28 15:09:11 +0530563 TWLFIXED_OF_MATCH("ti,twl4030-vintdig", VINTDIG),
564 TWLFIXED_OF_MATCH("ti,twl4030-vusb1v5", VUSB1V5),
565 TWLFIXED_OF_MATCH("ti,twl4030-vusb1v8", VUSB1V8),
566 TWLFIXED_OF_MATCH("ti,twl4030-vusb3v1", VUSB3V1),
Rajendra Nayak2098e952012-02-28 15:09:11 +0530567 {},
568};
569MODULE_DEVICE_TABLE(of, twl_of_match);
570
Bill Pembertona5023572012-11-19 13:22:22 -0500571static int twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800572{
Nicolae Rosia8313a4f2016-11-12 14:42:13 +0200573 int id;
David Brownellfa16a5c2009-02-08 10:37:06 -0800574 struct twlreg_info *info;
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000575 const struct twlreg_info *template;
David Brownellfa16a5c2009-02-08 10:37:06 -0800576 struct regulator_init_data *initdata;
577 struct regulation_constraints *c;
578 struct regulator_dev *rdev;
Rajendra Nayak2098e952012-02-28 15:09:11 +0530579 const struct of_device_id *match;
Mark Brownc172708d2012-04-04 00:50:22 +0100580 struct regulator_config config = { };
David Brownellfa16a5c2009-02-08 10:37:06 -0800581
Rajendra Nayak2098e952012-02-28 15:09:11 +0530582 match = of_match_device(twl_of_match, &pdev->dev);
Nicolae Rosia25d82332016-11-12 14:42:12 +0200583 if (!match)
584 return -ENODEV;
Axel Lin5ade3932012-04-09 22:32:49 +0800585
Nicolae Rosia25d82332016-11-12 14:42:12 +0200586 template = match->data;
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000587 if (!template)
David Brownellfa16a5c2009-02-08 10:37:06 -0800588 return -ENODEV;
589
Nicolae Rosia25d82332016-11-12 14:42:12 +0200590 id = template->desc.id;
591 initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
592 &template->desc);
David Brownellfa16a5c2009-02-08 10:37:06 -0800593 if (!initdata)
594 return -EINVAL;
595
Axel Lincd01e322014-06-16 09:44:20 +0800596 info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
Arnd Bergmann0ffff5a2012-08-14 12:41:50 +0000597 if (!info)
598 return -ENOMEM;
599
David Brownellfa16a5c2009-02-08 10:37:06 -0800600 /* Constrain board-specific capabilities according to what
601 * this driver and the chip itself can actually do.
602 */
603 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -0800604 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
605 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
606 | REGULATOR_CHANGE_MODE
607 | REGULATOR_CHANGE_STATUS;
Rajendra Nayak2098e952012-02-28 15:09:11 +0530608 switch (id) {
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +0200609 case TWL4030_REG_VIO:
610 case TWL4030_REG_VDD1:
611 case TWL4030_REG_VDD2:
612 case TWL4030_REG_VPLL1:
613 case TWL4030_REG_VINTANA1:
614 case TWL4030_REG_VINTANA2:
615 case TWL4030_REG_VINTDIG:
616 c->always_on = true;
617 break;
618 default:
619 break;
620 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800621
Mark Brownc172708d2012-04-04 00:50:22 +0100622 config.dev = &pdev->dev;
623 config.init_data = initdata;
624 config.driver_data = info;
625 config.of_node = pdev->dev.of_node;
626
Jingoo Han00ce0702013-09-30 09:59:04 +0900627 rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
David Brownellfa16a5c2009-02-08 10:37:06 -0800628 if (IS_ERR(rdev)) {
629 dev_err(&pdev->dev, "can't register %s, %ld\n",
630 info->desc.name, PTR_ERR(rdev));
631 return PTR_ERR(rdev);
632 }
633 platform_set_drvdata(pdev, rdev);
634
Nicolae Rosiacac28ae2016-11-12 14:42:15 +0200635 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP, info->remap);
Juha Keski-Saari30010fa2009-12-16 15:27:58 +0200636
David Brownellfa16a5c2009-02-08 10:37:06 -0800637 /* NOTE: many regulators support short-circuit IRQs (presentable
638 * as REGULATOR_OVER_CURRENT notifications?) configured via:
639 * - SC_CONFIG
640 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
641 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
642 * - IT_CONFIG
643 */
644
645 return 0;
646}
647
Nicolae Rosiacac28ae2016-11-12 14:42:15 +0200648MODULE_ALIAS("platform:twl4030_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -0800649
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100650static struct platform_driver twlreg_driver = {
651 .probe = twlreg_probe,
David Brownellfa16a5c2009-02-08 10:37:06 -0800652 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100653 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -0800654 */
Rajendra Nayak2098e952012-02-28 15:09:11 +0530655 .driver = {
Nicolae Rosiacac28ae2016-11-12 14:42:15 +0200656 .name = "twl4030_reg",
Rajendra Nayak2098e952012-02-28 15:09:11 +0530657 .of_match_table = of_match_ptr(twl_of_match),
658 },
David Brownellfa16a5c2009-02-08 10:37:06 -0800659};
660
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100661static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800662{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100663 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800664}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100665subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -0800666
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100667static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800668{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100669 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800670}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100671module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -0800672
Nicolae Rosiacac28ae2016-11-12 14:42:15 +0200673MODULE_DESCRIPTION("TWL4030 regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -0800674MODULE_LICENSE("GPL");