blob: f4b2c29d77e3302adc1451657179a2b8916bcdb9 [file] [log] [blame]
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001/*
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02002 *
3 * Handle TWL4030 Power initialization
4 *
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2006 Texas Instruments, Inc
7 *
8 * Written by Kalle Jokiniemi
9 * Peter De Schrijver <peter.de-schrijver@nokia.com>
10 * Several fixes by Amit Kucheria <amit.kucheria@verdurent.com>
11 *
12 * This file is subject to the terms and conditions of the GNU General
13 * Public License. See the file "COPYING" in the main directory of this
14 * archive for more details.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include <linux/module.h>
27#include <linux/pm.h>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010028#include <linux/i2c/twl.h>
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020029#include <linux/platform_device.h>
Florian Vaussardb0fc1da2013-06-18 15:17:58 +020030#include <linux/of.h>
Tony Lindgrene7cd1d12014-05-20 11:17:54 -070031#include <linux/of_device.h>
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020032
33#include <asm/mach-types.h>
34
35static u8 twl4030_start_script_address = 0x2b;
36
Tony Lindgren32057282014-05-20 11:17:53 -070037/* Register bits for P1, P2 and P3_SW_EVENTS */
38#define PWR_STOPON_PRWON BIT(6)
39#define PWR_STOPON_SYSEN BIT(5)
40#define PWR_ENABLE_WARMRESET BIT(4)
41#define PWR_LVL_WAKEUP BIT(3)
42#define PWR_DEVACT BIT(2)
43#define PWR_DEVSLP BIT(1)
44#define PWR_DEVOFF BIT(0)
45
Tony Lindgren481c7f82014-11-02 10:07:56 -080046/* Register bits for CFG_P1_TRANSITION (also for P2 and P3) */
47#define STARTON_SWBUG BIT(7) /* Start on watchdog */
48#define STARTON_VBUS BIT(5) /* Start on VBUS */
49#define STARTON_VBAT BIT(4) /* Start on battery insert */
50#define STARTON_RTC BIT(3) /* Start on RTC */
51#define STARTON_USB BIT(2) /* Start on USB host */
52#define STARTON_CHG BIT(1) /* Start on charger */
53#define STARTON_PWON BIT(0) /* Start on PWRON button */
54
Igor Grinberg26cc3ab2011-11-13 11:49:50 +020055#define SEQ_OFFSYNC (1 << 0)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020056
57#define PHY_TO_OFF_PM_MASTER(p) (p - 0x36)
58#define PHY_TO_OFF_PM_RECEIVER(p) (p - 0x5b)
59
60/* resource - hfclk */
61#define R_HFCLKOUT_DEV_GRP PHY_TO_OFF_PM_RECEIVER(0xe6)
62
63/* PM events */
64#define R_P1_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x46)
65#define R_P2_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x47)
66#define R_P3_SW_EVENTS PHY_TO_OFF_PM_MASTER(0x48)
67#define R_CFG_P1_TRANSITION PHY_TO_OFF_PM_MASTER(0x36)
68#define R_CFG_P2_TRANSITION PHY_TO_OFF_PM_MASTER(0x37)
69#define R_CFG_P3_TRANSITION PHY_TO_OFF_PM_MASTER(0x38)
70
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020071#define END_OF_SCRIPT 0x3f
72
73#define R_SEQ_ADD_A2S PHY_TO_OFF_PM_MASTER(0x55)
74#define R_SEQ_ADD_S2A12 PHY_TO_OFF_PM_MASTER(0x56)
75#define R_SEQ_ADD_S2A3 PHY_TO_OFF_PM_MASTER(0x57)
76#define R_SEQ_ADD_WARM PHY_TO_OFF_PM_MASTER(0x58)
77#define R_MEMORY_ADDRESS PHY_TO_OFF_PM_MASTER(0x59)
78#define R_MEMORY_DATA PHY_TO_OFF_PM_MASTER(0x5a)
79
Amit Kucheria890463f2009-10-19 15:10:48 +030080/* resource configuration registers
81 <RESOURCE>_DEV_GRP at address 'n+0'
82 <RESOURCE>_TYPE at address 'n+1'
83 <RESOURCE>_REMAP at address 'n+2'
84 <RESOURCE>_DEDICATED at address 'n+3'
85*/
Amit Kucheriae97d1542009-10-19 15:10:44 +030086#define DEV_GRP_OFFSET 0
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020087#define TYPE_OFFSET 1
Amit Kucheriab4ead612009-10-19 15:11:00 +030088#define REMAP_OFFSET 2
89#define DEDICATED_OFFSET 3
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020090
Amit Kucheriae97d1542009-10-19 15:10:44 +030091/* Bit positions in the registers */
Amit Kucheria890463f2009-10-19 15:10:48 +030092
93/* <RESOURCE>_DEV_GRP */
Amit Kucheriae97d1542009-10-19 15:10:44 +030094#define DEV_GRP_SHIFT 5
95#define DEV_GRP_MASK (7 << DEV_GRP_SHIFT)
Amit Kucheria890463f2009-10-19 15:10:48 +030096
97/* <RESOURCE>_TYPE */
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020098#define TYPE_SHIFT 0
99#define TYPE_MASK (7 << TYPE_SHIFT)
100#define TYPE2_SHIFT 3
101#define TYPE2_MASK (3 << TYPE2_SHIFT)
102
Amit Kucheriab4ead612009-10-19 15:11:00 +0300103/* <RESOURCE>_REMAP */
104#define SLEEP_STATE_SHIFT 0
105#define SLEEP_STATE_MASK (0xf << SLEEP_STATE_SHIFT)
106#define OFF_STATE_SHIFT 4
107#define OFF_STATE_MASK (0xf << OFF_STATE_SHIFT)
108
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200109static u8 res_config_addrs[] = {
110 [RES_VAUX1] = 0x17,
111 [RES_VAUX2] = 0x1b,
112 [RES_VAUX3] = 0x1f,
113 [RES_VAUX4] = 0x23,
114 [RES_VMMC1] = 0x27,
115 [RES_VMMC2] = 0x2b,
116 [RES_VPLL1] = 0x2f,
117 [RES_VPLL2] = 0x33,
118 [RES_VSIM] = 0x37,
119 [RES_VDAC] = 0x3b,
120 [RES_VINTANA1] = 0x3f,
121 [RES_VINTANA2] = 0x43,
122 [RES_VINTDIG] = 0x47,
123 [RES_VIO] = 0x4b,
124 [RES_VDD1] = 0x55,
125 [RES_VDD2] = 0x63,
126 [RES_VUSB_1V5] = 0x71,
127 [RES_VUSB_1V8] = 0x74,
128 [RES_VUSB_3V1] = 0x77,
129 [RES_VUSBCP] = 0x7a,
130 [RES_REGEN] = 0x7f,
131 [RES_NRES_PWRON] = 0x82,
132 [RES_CLKEN] = 0x85,
133 [RES_SYSEN] = 0x88,
134 [RES_HFCLKOUT] = 0x8b,
135 [RES_32KCLKOUT] = 0x8e,
136 [RES_RESET] = 0x91,
Lesly A Md7ac8292011-04-14 17:57:51 +0530137 [RES_MAIN_REF] = 0x94,
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200138};
139
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700140/*
141 * Usable values for .remap_sleep and .remap_off
142 * Based on table "5.3.3 Resource Operating modes"
143 */
144enum {
145 TWL_REMAP_OFF = 0,
146 TWL_REMAP_SLEEP = 8,
147 TWL_REMAP_ACTIVE = 9,
148};
149
150/*
151 * Macros to configure the PM register states for various resources.
152 * Note that we can make MSG_SINGULAR etc private to this driver once
153 * omap3 has been made DT only.
154 */
155#define TWL_DFLT_DELAY 2 /* typically 2 32 KiHz cycles */
Tony Lindgren76714d22014-05-20 11:17:54 -0700156#define TWL_DEV_GRP_P123 (DEV_GRP_P1 | DEV_GRP_P2 | DEV_GRP_P3)
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700157#define TWL_RESOURCE_SET(res, state) \
158 { MSG_SINGULAR(DEV_GRP_NULL, (res), (state)), TWL_DFLT_DELAY }
159#define TWL_RESOURCE_ON(res) TWL_RESOURCE_SET(res, RES_STATE_ACTIVE)
160#define TWL_RESOURCE_OFF(res) TWL_RESOURCE_SET(res, RES_STATE_OFF)
161#define TWL_RESOURCE_RESET(res) TWL_RESOURCE_SET(res, RES_STATE_WRST)
162/*
163 * It seems that type1 and type2 is just the resource init order
164 * number for the type1 and type2 group.
165 */
Tony Lindgren76714d22014-05-20 11:17:54 -0700166#define TWL_RESOURCE_SET_ACTIVE(res, state) \
167 { MSG_SINGULAR(DEV_GRP_NULL, (res), RES_STATE_ACTIVE), (state) }
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700168#define TWL_RESOURCE_GROUP_RESET(group, type1, type2) \
169 { MSG_BROADCAST(DEV_GRP_NULL, (group), (type1), (type2), \
170 RES_STATE_WRST), TWL_DFLT_DELAY }
Tony Lindgren76714d22014-05-20 11:17:54 -0700171#define TWL_RESOURCE_GROUP_SLEEP(group, type, type2) \
172 { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2), \
173 RES_STATE_SLEEP), TWL_DFLT_DELAY }
174#define TWL_RESOURCE_GROUP_ACTIVE(group, type, type2) \
175 { MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2), \
176 RES_STATE_ACTIVE), TWL_DFLT_DELAY }
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700177#define TWL_REMAP_SLEEP(res, devgrp, typ, typ2) \
178 { .resource = (res), .devgroup = (devgrp), \
179 .type = (typ), .type2 = (typ2), \
180 .remap_off = TWL_REMAP_OFF, \
181 .remap_sleep = TWL_REMAP_SLEEP, }
Tony Lindgren76714d22014-05-20 11:17:54 -0700182#define TWL_REMAP_OFF(res, devgrp, typ, typ2) \
183 { .resource = (res), .devgroup = (devgrp), \
184 .type = (typ), .type2 = (typ2), \
185 .remap_off = TWL_REMAP_OFF, .remap_sleep = TWL_REMAP_OFF, }
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700186
Bill Pembertonf791be42012-11-19 13:23:04 -0500187static int twl4030_write_script_byte(u8 address, u8 byte)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200188{
189 int err;
190
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100191 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_MEMORY_ADDRESS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200192 if (err)
193 goto out;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100194 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, byte, R_MEMORY_DATA);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200195out:
196 return err;
197}
198
Bill Pembertonf791be42012-11-19 13:23:04 -0500199static int twl4030_write_script_ins(u8 address, u16 pmb_message,
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200200 u8 delay, u8 next)
201{
202 int err;
203
204 address *= 4;
205 err = twl4030_write_script_byte(address++, pmb_message >> 8);
206 if (err)
207 goto out;
208 err = twl4030_write_script_byte(address++, pmb_message & 0xff);
209 if (err)
210 goto out;
211 err = twl4030_write_script_byte(address++, delay);
212 if (err)
213 goto out;
214 err = twl4030_write_script_byte(address++, next);
215out:
216 return err;
217}
218
Bill Pembertonf791be42012-11-19 13:23:04 -0500219static int twl4030_write_script(u8 address, struct twl4030_ins *script,
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200220 int len)
221{
Arnd Bergmannf65e9ea2013-01-25 14:14:26 +0000222 int err = -EINVAL;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200223
224 for (; len; len--, address++, script++) {
225 if (len == 1) {
226 err = twl4030_write_script_ins(address,
227 script->pmb_message,
228 script->delay,
229 END_OF_SCRIPT);
230 if (err)
231 break;
232 } else {
233 err = twl4030_write_script_ins(address,
234 script->pmb_message,
235 script->delay,
236 address + 1);
237 if (err)
238 break;
239 }
240 }
241 return err;
242}
243
Bill Pembertonf791be42012-11-19 13:23:04 -0500244static int twl4030_config_wakeup3_sequence(u8 address)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200245{
246 int err;
247 u8 data;
248
249 /* Set SLEEP to ACTIVE SEQ address for P3 */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100250 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A3);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200251 if (err)
252 goto out;
253
254 /* P3 LVL_WAKEUP should be on LEVEL */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100255 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P3_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200256 if (err)
257 goto out;
Tony Lindgren32057282014-05-20 11:17:53 -0700258 data |= PWR_LVL_WAKEUP;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100259 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P3_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200260out:
261 if (err)
262 pr_err("TWL4030 wakeup sequence for P3 config error\n");
263 return err;
264}
265
Tony Lindgren5c188d72015-04-27 10:18:14 -0700266static int
267twl4030_config_wakeup12_sequence(const struct twl4030_power_data *pdata,
268 u8 address)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200269{
270 int err = 0;
271 u8 data;
272
273 /* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100274 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A12);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200275 if (err)
276 goto out;
277
278 /* P1/P2 LVL_WAKEUP should be on LEVEL */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100279 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P1_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200280 if (err)
281 goto out;
282
Tony Lindgren32057282014-05-20 11:17:53 -0700283 data |= PWR_LVL_WAKEUP;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100284 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P1_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200285 if (err)
286 goto out;
287
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100288 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P2_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200289 if (err)
290 goto out;
291
Tony Lindgren32057282014-05-20 11:17:53 -0700292 data |= PWR_LVL_WAKEUP;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100293 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P2_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200294 if (err)
295 goto out;
296
Tony Lindgren5c188d72015-04-27 10:18:14 -0700297 if (pdata->ac_charger_quirk || machine_is_omap_3430sdp() ||
298 machine_is_omap_ldp()) {
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200299 /* Disabling AC charger effect on sleep-active transitions */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100300 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data,
301 R_CFG_P1_TRANSITION);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200302 if (err)
303 goto out;
Tony Lindgren5c188d72015-04-27 10:18:14 -0700304 data &= ~STARTON_CHG;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100305 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data,
306 R_CFG_P1_TRANSITION);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200307 if (err)
308 goto out;
309 }
310
311out:
312 if (err)
313 pr_err("TWL4030 wakeup sequence for P1 and P2" \
314 "config error\n");
315 return err;
316}
317
Bill Pembertonf791be42012-11-19 13:23:04 -0500318static int twl4030_config_sleep_sequence(u8 address)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200319{
320 int err;
321
322 /* Set ACTIVE to SLEEP SEQ address in T2 memory*/
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100323 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_A2S);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200324
325 if (err)
326 pr_err("TWL4030 sleep sequence config error\n");
327
328 return err;
329}
330
Bill Pembertonf791be42012-11-19 13:23:04 -0500331static int twl4030_config_warmreset_sequence(u8 address)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200332{
333 int err;
334 u8 rd_data;
335
336 /* Set WARM RESET SEQ address for P1 */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100337 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_WARM);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200338 if (err)
339 goto out;
340
341 /* P1/P2/P3 enable WARMRESET */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100342 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P1_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200343 if (err)
344 goto out;
345
Tony Lindgren32057282014-05-20 11:17:53 -0700346 rd_data |= PWR_ENABLE_WARMRESET;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100347 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P1_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200348 if (err)
349 goto out;
350
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100351 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P2_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200352 if (err)
353 goto out;
354
Tony Lindgren32057282014-05-20 11:17:53 -0700355 rd_data |= PWR_ENABLE_WARMRESET;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100356 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P2_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200357 if (err)
358 goto out;
359
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100360 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P3_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200361 if (err)
362 goto out;
363
Tony Lindgren32057282014-05-20 11:17:53 -0700364 rd_data |= PWR_ENABLE_WARMRESET;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100365 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P3_SW_EVENTS);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200366out:
367 if (err)
368 pr_err("TWL4030 warmreset seq config error\n");
369 return err;
370}
371
Bill Pembertonf791be42012-11-19 13:23:04 -0500372static int twl4030_configure_resource(struct twl4030_resconfig *rconfig)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200373{
374 int rconfig_addr;
375 int err;
376 u8 type;
377 u8 grp;
Amit Kucheriab4ead612009-10-19 15:11:00 +0300378 u8 remap;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200379
380 if (rconfig->resource > TOTAL_RESOURCES) {
381 pr_err("TWL4030 Resource %d does not exist\n",
382 rconfig->resource);
383 return -EINVAL;
384 }
385
386 rconfig_addr = res_config_addrs[rconfig->resource];
387
388 /* Set resource group */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100389 err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &grp,
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100390 rconfig_addr + DEV_GRP_OFFSET);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200391 if (err) {
392 pr_err("TWL4030 Resource %d group could not be read\n",
393 rconfig->resource);
394 return err;
395 }
396
Aaro Koskinen56baa662009-10-19 21:24:02 +0200397 if (rconfig->devgroup != TWL4030_RESCONFIG_UNDEF) {
Amit Kucheriae97d1542009-10-19 15:10:44 +0300398 grp &= ~DEV_GRP_MASK;
399 grp |= rconfig->devgroup << DEV_GRP_SHIFT;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100400 err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100401 grp, rconfig_addr + DEV_GRP_OFFSET);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200402 if (err < 0) {
403 pr_err("TWL4030 failed to program devgroup\n");
404 return err;
405 }
406 }
407
408 /* Set resource types */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100409 err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &type,
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200410 rconfig_addr + TYPE_OFFSET);
411 if (err < 0) {
412 pr_err("TWL4030 Resource %d type could not be read\n",
413 rconfig->resource);
414 return err;
415 }
416
Aaro Koskinen56baa662009-10-19 21:24:02 +0200417 if (rconfig->type != TWL4030_RESCONFIG_UNDEF) {
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200418 type &= ~TYPE_MASK;
419 type |= rconfig->type << TYPE_SHIFT;
420 }
421
Aaro Koskinen56baa662009-10-19 21:24:02 +0200422 if (rconfig->type2 != TWL4030_RESCONFIG_UNDEF) {
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200423 type &= ~TYPE2_MASK;
424 type |= rconfig->type2 << TYPE2_SHIFT;
425 }
426
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100427 err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200428 type, rconfig_addr + TYPE_OFFSET);
429 if (err < 0) {
430 pr_err("TWL4030 failed to program resource type\n");
431 return err;
432 }
433
Amit Kucheriab4ead612009-10-19 15:11:00 +0300434 /* Set remap states */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100435 err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &remap,
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100436 rconfig_addr + REMAP_OFFSET);
Amit Kucheriab4ead612009-10-19 15:11:00 +0300437 if (err < 0) {
438 pr_err("TWL4030 Resource %d remap could not be read\n",
439 rconfig->resource);
440 return err;
441 }
442
Amit Kucheria53cf9a62009-10-21 14:49:22 +0300443 if (rconfig->remap_off != TWL4030_RESCONFIG_UNDEF) {
Amit Kucheriab4ead612009-10-19 15:11:00 +0300444 remap &= ~OFF_STATE_MASK;
445 remap |= rconfig->remap_off << OFF_STATE_SHIFT;
446 }
447
Amit Kucheria53cf9a62009-10-21 14:49:22 +0300448 if (rconfig->remap_sleep != TWL4030_RESCONFIG_UNDEF) {
Amit Kucheriab4ead612009-10-19 15:11:00 +0300449 remap &= ~SLEEP_STATE_MASK;
Mike Turquette1ea933f2010-02-05 09:51:37 +0100450 remap |= rconfig->remap_sleep << SLEEP_STATE_SHIFT;
Amit Kucheriab4ead612009-10-19 15:11:00 +0300451 }
452
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100453 err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100454 remap,
455 rconfig_addr + REMAP_OFFSET);
Amit Kucheriab4ead612009-10-19 15:11:00 +0300456 if (err < 0) {
457 pr_err("TWL4030 failed to program remap\n");
458 return err;
459 }
460
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200461 return 0;
462}
463
Tony Lindgren5c188d72015-04-27 10:18:14 -0700464static int load_twl4030_script(const struct twl4030_power_data *pdata,
465 struct twl4030_script *tscript,
466 u8 address)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200467{
468 int err;
Amit Kucheria75a74562009-08-17 17:01:56 +0300469 static int order;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200470
471 /* Make sure the script isn't going beyond last valid address (0x3f) */
472 if ((address + tscript->size) > END_OF_SCRIPT) {
473 pr_err("TWL4030 scripts too big error\n");
474 return -EINVAL;
475 }
476
477 err = twl4030_write_script(address, tscript->script, tscript->size);
478 if (err)
479 goto out;
480
481 if (tscript->flags & TWL4030_WRST_SCRIPT) {
482 err = twl4030_config_warmreset_sequence(address);
483 if (err)
484 goto out;
485 }
486 if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
Tony Lindgrenfc7d76e2014-05-13 18:34:04 -0700487 /* Reset any existing sleep script to avoid hangs on reboot */
488 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
489 R_SEQ_ADD_A2S);
490 if (err)
491 goto out;
492
Tony Lindgren5c188d72015-04-27 10:18:14 -0700493 err = twl4030_config_wakeup12_sequence(pdata, address);
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200494 if (err)
495 goto out;
Amit Kucheria75a74562009-08-17 17:01:56 +0300496 order = 1;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200497 }
498 if (tscript->flags & TWL4030_WAKEUP3_SCRIPT) {
499 err = twl4030_config_wakeup3_sequence(address);
500 if (err)
501 goto out;
502 }
Lesly A Mc62dd362011-04-14 17:57:49 +0530503 if (tscript->flags & TWL4030_SLEEP_SCRIPT) {
Lesly A M1f968ff2011-04-14 17:57:50 +0530504 if (!order)
Joe Perches81d30ed2017-02-16 23:11:36 -0800505 pr_warn("TWL4030: Bad order of scripts (sleep script before wakeup) Leads to boot failure on some boards\n");
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200506 err = twl4030_config_sleep_sequence(address);
Lesly A Mc62dd362011-04-14 17:57:49 +0530507 }
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200508out:
509 return err;
510}
511
Mike Turquette11a441c2010-02-22 11:16:30 -0600512int twl4030_remove_script(u8 flags)
513{
514 int err = 0;
515
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100516 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
517 TWL4030_PM_MASTER_PROTECT_KEY);
Mike Turquette11a441c2010-02-22 11:16:30 -0600518 if (err) {
519 pr_err("twl4030: unable to unlock PROTECT_KEY\n");
520 return err;
521 }
522
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100523 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
524 TWL4030_PM_MASTER_PROTECT_KEY);
Mike Turquette11a441c2010-02-22 11:16:30 -0600525 if (err) {
526 pr_err("twl4030: unable to unlock PROTECT_KEY\n");
527 return err;
528 }
529
530 if (flags & TWL4030_WRST_SCRIPT) {
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100531 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
532 R_SEQ_ADD_WARM);
Mike Turquette11a441c2010-02-22 11:16:30 -0600533 if (err)
534 return err;
535 }
536 if (flags & TWL4030_WAKEUP12_SCRIPT) {
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100537 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
538 R_SEQ_ADD_S2A12);
Lesly A Meac78a22011-05-04 17:38:53 +0530539 if (err)
Mike Turquette11a441c2010-02-22 11:16:30 -0600540 return err;
541 }
542 if (flags & TWL4030_WAKEUP3_SCRIPT) {
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100543 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
544 R_SEQ_ADD_S2A3);
Mike Turquette11a441c2010-02-22 11:16:30 -0600545 if (err)
546 return err;
547 }
548 if (flags & TWL4030_SLEEP_SCRIPT) {
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100549 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
550 R_SEQ_ADD_A2S);
Mike Turquette11a441c2010-02-22 11:16:30 -0600551 if (err)
552 return err;
553 }
554
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100555 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
556 TWL4030_PM_MASTER_PROTECT_KEY);
Mike Turquette11a441c2010-02-22 11:16:30 -0600557 if (err)
558 pr_err("TWL4030 Unable to relock registers\n");
559
560 return err;
561}
562
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700563static int
564twl4030_power_configure_scripts(const struct twl4030_power_data *pdata)
Florian Vaussardf58cb402013-06-18 15:17:57 +0200565{
566 int err;
567 int i;
568 u8 address = twl4030_start_script_address;
569
570 for (i = 0; i < pdata->num; i++) {
Tony Lindgren5c188d72015-04-27 10:18:14 -0700571 err = load_twl4030_script(pdata, pdata->scripts[i], address);
Florian Vaussardf58cb402013-06-18 15:17:57 +0200572 if (err)
573 return err;
574 address += pdata->scripts[i]->size;
575 }
576
577 return 0;
578}
579
Tony Lindgren482e7db2014-05-20 11:17:54 -0700580static void twl4030_patch_rconfig(struct twl4030_resconfig *common,
581 struct twl4030_resconfig *board)
582{
583 while (common->resource) {
584 struct twl4030_resconfig *b = board;
585
586 while (b->resource) {
587 if (b->resource == common->resource) {
588 *common = *b;
589 break;
590 }
591 b++;
592 }
593 common++;
594 }
595}
596
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700597static int
598twl4030_power_configure_resources(const struct twl4030_power_data *pdata)
Florian Vaussardf58cb402013-06-18 15:17:57 +0200599{
600 struct twl4030_resconfig *resconfig = pdata->resource_config;
Tony Lindgren482e7db2014-05-20 11:17:54 -0700601 struct twl4030_resconfig *boardconf = pdata->board_config;
Florian Vaussardf58cb402013-06-18 15:17:57 +0200602 int err;
603
604 if (resconfig) {
Tony Lindgren482e7db2014-05-20 11:17:54 -0700605 if (boardconf)
606 twl4030_patch_rconfig(resconfig, boardconf);
607
Florian Vaussardf58cb402013-06-18 15:17:57 +0200608 while (resconfig->resource) {
609 err = twl4030_configure_resource(resconfig);
610 if (err)
611 return err;
612 resconfig++;
613 }
614 }
615
616 return 0;
617}
618
Tony Lindgren481c7f82014-11-02 10:07:56 -0800619static int twl4030_starton_mask_and_set(u8 bitmask, u8 bitvalues)
620{
621 u8 regs[3] = { TWL4030_PM_MASTER_CFG_P1_TRANSITION,
622 TWL4030_PM_MASTER_CFG_P2_TRANSITION,
623 TWL4030_PM_MASTER_CFG_P3_TRANSITION, };
624 u8 val;
625 int i, err;
626
627 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
628 TWL4030_PM_MASTER_PROTECT_KEY);
629 if (err)
630 goto relock;
631 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
632 TWL4030_PM_MASTER_KEY_CFG2,
633 TWL4030_PM_MASTER_PROTECT_KEY);
634 if (err)
635 goto relock;
636
637 for (i = 0; i < sizeof(regs); i++) {
638 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER,
639 &val, regs[i]);
640 if (err)
641 break;
642 val = (~bitmask & val) | (bitmask & bitvalues);
643 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
644 val, regs[i]);
645 if (err)
646 break;
647 }
648
649 if (err)
650 pr_err("TWL4030 Register access failed: %i\n", err);
651
652relock:
653 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
654 TWL4030_PM_MASTER_PROTECT_KEY);
655}
656
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200657/*
658 * In master mode, start the power off sequence.
659 * After a successful execution, TWL shuts down the power to the SoC
660 * and all peripherals connected to it.
661 */
662void twl4030_power_off(void)
663{
664 int err;
665
Tony Lindgren481c7f82014-11-02 10:07:56 -0800666 /* Disable start on charger or VBUS as it can break poweroff */
667 err = twl4030_starton_mask_and_set(STARTON_VBUS | STARTON_CHG, 0);
668 if (err)
669 pr_err("TWL4030 Unable to configure start-up\n");
670
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100671 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, PWR_DEVOFF,
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200672 TWL4030_PM_MASTER_P1_SW_EVENTS);
673 if (err)
674 pr_err("TWL4030 Unable to power off\n");
675}
676
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700677static bool twl4030_power_use_poweroff(const struct twl4030_power_data *pdata,
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200678 struct device_node *node)
679{
680 if (pdata && pdata->use_poweroff)
681 return true;
682
Nishanth Menonfecc4452014-09-17 07:34:12 -0500683 if (of_property_read_bool(node, "ti,system-power-controller"))
684 return true;
685
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200686 if (of_property_read_bool(node, "ti,use_poweroff"))
687 return true;
688
689 return false;
690}
691
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700692#ifdef CONFIG_OF
693
694/* Generic warm reset configuration for omap3 */
695
696static struct twl4030_ins omap3_wrst_seq[] = {
697 TWL_RESOURCE_OFF(RES_NRES_PWRON),
698 TWL_RESOURCE_OFF(RES_RESET),
699 TWL_RESOURCE_RESET(RES_MAIN_REF),
700 TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R2),
701 TWL_RESOURCE_RESET(RES_VUSB_3V1),
Adam Fordad48ed02017-01-29 06:40:15 -0600702 TWL_RESOURCE_RESET(RES_VMMC1),
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700703 TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R1),
704 TWL_RESOURCE_GROUP_RESET(RES_GRP_RC, RES_TYPE_ALL, RES_TYPE2_R0),
705 TWL_RESOURCE_ON(RES_RESET),
706 TWL_RESOURCE_ON(RES_NRES_PWRON),
707};
708
709static struct twl4030_script omap3_wrst_script = {
710 .script = omap3_wrst_seq,
711 .size = ARRAY_SIZE(omap3_wrst_seq),
712 .flags = TWL4030_WRST_SCRIPT,
713};
714
715static struct twl4030_script *omap3_reset_scripts[] = {
716 &omap3_wrst_script,
717};
718
719static struct twl4030_resconfig omap3_rconfig[] = {
720 TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, -1, -1),
721 TWL_REMAP_SLEEP(RES_VDD1, DEV_GRP_P1, -1, -1),
722 TWL_REMAP_SLEEP(RES_VDD2, DEV_GRP_P1, -1, -1),
723 { 0, 0 },
724};
725
726static struct twl4030_power_data omap3_reset = {
727 .scripts = omap3_reset_scripts,
728 .num = ARRAY_SIZE(omap3_reset_scripts),
729 .resource_config = omap3_rconfig,
730};
731
Tony Lindgren76714d22014-05-20 11:17:54 -0700732/* Recommended generic default idle configuration for off-idle */
733
734/* Broadcast message to put res to sleep */
735static struct twl4030_ins omap3_idle_sleep_on_seq[] = {
736 TWL_RESOURCE_GROUP_SLEEP(RES_GRP_ALL, RES_TYPE_ALL, 0),
737};
738
739static struct twl4030_script omap3_idle_sleep_on_script = {
740 .script = omap3_idle_sleep_on_seq,
741 .size = ARRAY_SIZE(omap3_idle_sleep_on_seq),
742 .flags = TWL4030_SLEEP_SCRIPT,
743};
744
745/* Broadcast message to put res to active */
746static struct twl4030_ins omap3_idle_wakeup_p12_seq[] = {
747 TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
748};
749
750static struct twl4030_script omap3_idle_wakeup_p12_script = {
751 .script = omap3_idle_wakeup_p12_seq,
752 .size = ARRAY_SIZE(omap3_idle_wakeup_p12_seq),
753 .flags = TWL4030_WAKEUP12_SCRIPT,
754};
755
756/* Broadcast message to put res to active */
757static struct twl4030_ins omap3_idle_wakeup_p3_seq[] = {
758 TWL_RESOURCE_SET_ACTIVE(RES_CLKEN, 0x37),
759 TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
760};
761
762static struct twl4030_script omap3_idle_wakeup_p3_script = {
763 .script = omap3_idle_wakeup_p3_seq,
764 .size = ARRAY_SIZE(omap3_idle_wakeup_p3_seq),
765 .flags = TWL4030_WAKEUP3_SCRIPT,
766};
767
768static struct twl4030_script *omap3_idle_scripts[] = {
769 &omap3_idle_wakeup_p12_script,
770 &omap3_idle_wakeup_p3_script,
771 &omap3_wrst_script,
772 &omap3_idle_sleep_on_script,
773};
774
775/*
776 * Recommended configuration based on "Recommended Sleep
777 * Sequences for the Zoom Platform":
778 * http://omappedia.com/wiki/File:Recommended_Sleep_Sequences_Zoom.pdf
779 * Note that the type1 and type2 seem to be just the init order number
780 * for type1 and type2 groups as specified in the document mentioned
781 * above.
782 */
783static struct twl4030_resconfig omap3_idle_rconfig[] = {
Tony Lindgrendaebabd2014-08-19 08:24:05 -0700784 TWL_REMAP_SLEEP(RES_VAUX1, TWL4030_RESCONFIG_UNDEF, 0, 0),
785 TWL_REMAP_SLEEP(RES_VAUX2, TWL4030_RESCONFIG_UNDEF, 0, 0),
786 TWL_REMAP_SLEEP(RES_VAUX3, TWL4030_RESCONFIG_UNDEF, 0, 0),
787 TWL_REMAP_SLEEP(RES_VAUX4, TWL4030_RESCONFIG_UNDEF, 0, 0),
788 TWL_REMAP_SLEEP(RES_VMMC1, TWL4030_RESCONFIG_UNDEF, 0, 0),
789 TWL_REMAP_SLEEP(RES_VMMC2, TWL4030_RESCONFIG_UNDEF, 0, 0),
Tony Lindgren76714d22014-05-20 11:17:54 -0700790 TWL_REMAP_OFF(RES_VPLL1, DEV_GRP_P1, 3, 1),
791 TWL_REMAP_SLEEP(RES_VPLL2, DEV_GRP_P1, 0, 0),
Tony Lindgrendaebabd2014-08-19 08:24:05 -0700792 TWL_REMAP_SLEEP(RES_VSIM, TWL4030_RESCONFIG_UNDEF, 0, 0),
793 TWL_REMAP_SLEEP(RES_VDAC, TWL4030_RESCONFIG_UNDEF, 0, 0),
Tony Lindgren76714d22014-05-20 11:17:54 -0700794 TWL_REMAP_SLEEP(RES_VINTANA1, TWL_DEV_GRP_P123, 1, 2),
795 TWL_REMAP_SLEEP(RES_VINTANA2, TWL_DEV_GRP_P123, 0, 2),
796 TWL_REMAP_SLEEP(RES_VINTDIG, TWL_DEV_GRP_P123, 1, 2),
797 TWL_REMAP_SLEEP(RES_VIO, TWL_DEV_GRP_P123, 2, 2),
798 TWL_REMAP_OFF(RES_VDD1, DEV_GRP_P1, 4, 1),
799 TWL_REMAP_OFF(RES_VDD2, DEV_GRP_P1, 3, 1),
Tony Lindgrendaebabd2014-08-19 08:24:05 -0700800 TWL_REMAP_SLEEP(RES_VUSB_1V5, TWL4030_RESCONFIG_UNDEF, 0, 0),
801 TWL_REMAP_SLEEP(RES_VUSB_1V8, TWL4030_RESCONFIG_UNDEF, 0, 0),
Tony Lindgren76714d22014-05-20 11:17:54 -0700802 TWL_REMAP_SLEEP(RES_VUSB_3V1, TWL_DEV_GRP_P123, 0, 0),
803 /* Resource #20 USB charge pump skipped */
804 TWL_REMAP_SLEEP(RES_REGEN, TWL_DEV_GRP_P123, 2, 1),
805 TWL_REMAP_SLEEP(RES_NRES_PWRON, TWL_DEV_GRP_P123, 0, 1),
806 TWL_REMAP_SLEEP(RES_CLKEN, TWL_DEV_GRP_P123, 3, 2),
807 TWL_REMAP_SLEEP(RES_SYSEN, TWL_DEV_GRP_P123, 6, 1),
808 TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, 0, 2),
809 TWL_REMAP_SLEEP(RES_32KCLKOUT, TWL_DEV_GRP_P123, 0, 0),
810 TWL_REMAP_SLEEP(RES_RESET, TWL_DEV_GRP_P123, 6, 0),
811 TWL_REMAP_SLEEP(RES_MAIN_REF, TWL_DEV_GRP_P123, 0, 0),
812 { /* Terminator */ },
813};
814
815static struct twl4030_power_data omap3_idle = {
816 .scripts = omap3_idle_scripts,
817 .num = ARRAY_SIZE(omap3_idle_scripts),
818 .resource_config = omap3_idle_rconfig,
819};
820
Tony Lindgren43fef472014-05-13 18:34:09 -0700821/* Disable 32 KiHz oscillator during idle */
822static struct twl4030_resconfig osc_off_rconfig[] = {
823 TWL_REMAP_OFF(RES_CLKEN, DEV_GRP_P1 | DEV_GRP_P3, 3, 2),
824 { /* Terminator */ },
825};
826
827static struct twl4030_power_data osc_off_idle = {
828 .scripts = omap3_idle_scripts,
829 .num = ARRAY_SIZE(omap3_idle_scripts),
830 .resource_config = omap3_idle_rconfig,
831 .board_config = osc_off_rconfig,
832};
833
Tony Lindgren5c188d72015-04-27 10:18:14 -0700834static struct twl4030_power_data omap3_idle_ac_quirk = {
835 .scripts = omap3_idle_scripts,
836 .num = ARRAY_SIZE(omap3_idle_scripts),
837 .resource_config = omap3_idle_rconfig,
838 .ac_charger_quirk = true,
839};
840
841static struct twl4030_power_data omap3_idle_ac_quirk_osc_off = {
842 .scripts = omap3_idle_scripts,
843 .num = ARRAY_SIZE(omap3_idle_scripts),
844 .resource_config = omap3_idle_rconfig,
845 .board_config = osc_off_rconfig,
846 .ac_charger_quirk = true,
847};
848
Fabian Frederick908725d2015-03-16 20:21:36 +0100849static const struct of_device_id twl4030_power_of_match[] = {
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700850 {
Tony Lindgren1b9b46d2014-11-02 10:09:38 -0800851 .compatible = "ti,twl4030-power",
852 },
853 {
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700854 .compatible = "ti,twl4030-power-reset",
855 .data = &omap3_reset,
856 },
Tony Lindgren76714d22014-05-20 11:17:54 -0700857 {
858 .compatible = "ti,twl4030-power-idle",
859 .data = &omap3_idle,
860 },
Tony Lindgren43fef472014-05-13 18:34:09 -0700861 {
862 .compatible = "ti,twl4030-power-idle-osc-off",
863 .data = &osc_off_idle,
864 },
Tony Lindgren5c188d72015-04-27 10:18:14 -0700865 {
866 .compatible = "ti,twl4030-power-omap3-sdp",
867 .data = &omap3_idle_ac_quirk,
868 },
869 {
870 .compatible = "ti,twl4030-power-omap3-ldp",
871 .data = &omap3_idle_ac_quirk_osc_off,
872 },
873 {
874 .compatible = "ti,twl4030-power-omap3-evm",
875 .data = &omap3_idle_ac_quirk,
876 },
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700877 { },
878};
879MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
880#endif /* CONFIG_OF */
881
Jingoo Hanfae01582013-08-01 10:52:55 +0900882static int twl4030_power_probe(struct platform_device *pdev)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200883{
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700884 const struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200885 struct device_node *node = pdev->dev.of_node;
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700886 const struct of_device_id *match;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200887 int err = 0;
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200888 int err2 = 0;
Florian Vaussardf58cb402013-06-18 15:17:57 +0200889 u8 val;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200890
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200891 if (!pdata && !node) {
892 dev_err(&pdev->dev, "Platform data is missing\n");
893 return -EINVAL;
894 }
895
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100896 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
897 TWL4030_PM_MASTER_PROTECT_KEY);
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200898 err |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
899 TWL4030_PM_MASTER_KEY_CFG2,
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100900 TWL4030_PM_MASTER_PROTECT_KEY);
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200901
902 if (err) {
903 pr_err("TWL4030 Unable to unlock registers\n");
904 return err;
905 }
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200906
Tony Lindgrene7cd1d12014-05-20 11:17:54 -0700907 match = of_match_device(of_match_ptr(twl4030_power_of_match),
908 &pdev->dev);
909 if (match && match->data)
910 pdata = match->data;
911
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200912 if (pdata) {
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200913 err = twl4030_power_configure_scripts(pdata);
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200914 if (err) {
915 pr_err("TWL4030 failed to load scripts\n");
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200916 goto relock;
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200917 }
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200918 err = twl4030_power_configure_resources(pdata);
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200919 if (err) {
920 pr_err("TWL4030 failed to configure resource\n");
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200921 goto relock;
Florian Vaussarde77a4c22013-06-18 15:17:59 +0200922 }
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200923 }
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200924
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200925 /* Board has to be wired properly to use this feature */
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200926 if (twl4030_power_use_poweroff(pdata, node) && !pm_power_off) {
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200927 /* Default for SEQ_OFFSYNC is set, lets ensure this */
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100928 err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200929 TWL4030_PM_MASTER_CFG_P123_TRANSITION);
930 if (err) {
Joe Perches81d30ed2017-02-16 23:11:36 -0800931 pr_warn("TWL4030 Unable to read registers\n");
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200932 } else if (!(val & SEQ_OFFSYNC)) {
933 val |= SEQ_OFFSYNC;
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100934 err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
Igor Grinberg26cc3ab2011-11-13 11:49:50 +0200935 TWL4030_PM_MASTER_CFG_P123_TRANSITION);
936 if (err) {
937 pr_err("TWL4030 Unable to setup SEQ_OFFSYNC\n");
938 goto relock;
939 }
940 }
941
942 pm_power_off = twl4030_power_off;
943 }
944
945relock:
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200946 err2 = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
Peter Ujfalusi4850f122012-11-13 09:28:52 +0100947 TWL4030_PM_MASTER_PROTECT_KEY);
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200948 if (err2) {
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200949 pr_err("TWL4030 Unable to relock registers\n");
Florian Vaussardcb3cabd2013-06-18 15:18:00 +0200950 return err2;
951 }
952
Florian Vaussard637d6892013-06-18 15:17:56 +0200953 return err;
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200954}
Florian Vaussard637d6892013-06-18 15:17:56 +0200955
956static int twl4030_power_remove(struct platform_device *pdev)
957{
958 return 0;
959}
960
961static struct platform_driver twl4030_power_driver = {
962 .driver = {
963 .name = "twl4030_power",
Florian Vaussardb0fc1da2013-06-18 15:17:58 +0200964 .of_match_table = of_match_ptr(twl4030_power_of_match),
Florian Vaussard637d6892013-06-18 15:17:56 +0200965 },
966 .probe = twl4030_power_probe,
967 .remove = twl4030_power_remove,
968};
969
970module_platform_driver(twl4030_power_driver);
971
972MODULE_AUTHOR("Nokia Corporation");
973MODULE_AUTHOR("Texas Instruments, Inc.");
974MODULE_DESCRIPTION("Power management for TWL4030");
975MODULE_LICENSE("GPL");
976MODULE_ALIAS("platform:twl4030_power");