blob: 9f01aef539ddd7a21a3958548b0c856b4138fff6 [file] [log] [blame]
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001/*
2 * Copyright (C) 2004 Texas Instruments, Inc.
3 *
4 * Some parts based tps65010.c:
5 * Copyright (C) 2004 Texas Instruments and
6 * Copyright (C) 2004-2005 David Brownell
7 *
8 * Some parts based on tlv320aic24.c:
9 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
10 *
11 * Changes for interrupt handling and clean-up by
12 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
13 * Cleanup and generalized support for voltage setting by
14 * Juha Yrjola
15 * Added support for controlling VCORE and regulator sleep states,
16 * Amit Kucheria <amit.kucheria@nokia.com>
17 * Copyright (C) 2005, 2006 Nokia Corporation
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33
34#include <linux/module.h>
35#include <linux/i2c.h>
36#include <linux/interrupt.h>
37#include <linux/sched.h>
38#include <linux/mutex.h>
39#include <linux/workqueue.h>
40#include <linux/delay.h>
41#include <linux/rtc.h>
42#include <linux/bcd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Tony Lindgren7bd3b612012-10-15 13:53:41 -070044#include <linux/mfd/menelaus.h>
Tony Lindgren0c4a59f2007-07-17 04:06:09 -070045
Tony Lindgren0c4a59f2007-07-17 04:06:09 -070046#include <asm/mach/irq.h>
47
Russell King1bc857f2011-07-26 10:54:55 +010048#include <asm/gpio.h>
Tony Lindgren0c4a59f2007-07-17 04:06:09 -070049
50#define DRIVER_NAME "menelaus"
51
Tony Lindgren0c4a59f2007-07-17 04:06:09 -070052#define MENELAUS_I2C_ADDRESS 0x72
53
54#define MENELAUS_REV 0x01
55#define MENELAUS_VCORE_CTRL1 0x02
56#define MENELAUS_VCORE_CTRL2 0x03
57#define MENELAUS_VCORE_CTRL3 0x04
58#define MENELAUS_VCORE_CTRL4 0x05
59#define MENELAUS_VCORE_CTRL5 0x06
60#define MENELAUS_DCDC_CTRL1 0x07
61#define MENELAUS_DCDC_CTRL2 0x08
62#define MENELAUS_DCDC_CTRL3 0x09
63#define MENELAUS_LDO_CTRL1 0x0A
64#define MENELAUS_LDO_CTRL2 0x0B
65#define MENELAUS_LDO_CTRL3 0x0C
66#define MENELAUS_LDO_CTRL4 0x0D
67#define MENELAUS_LDO_CTRL5 0x0E
68#define MENELAUS_LDO_CTRL6 0x0F
69#define MENELAUS_LDO_CTRL7 0x10
70#define MENELAUS_LDO_CTRL8 0x11
71#define MENELAUS_SLEEP_CTRL1 0x12
72#define MENELAUS_SLEEP_CTRL2 0x13
73#define MENELAUS_DEVICE_OFF 0x14
74#define MENELAUS_OSC_CTRL 0x15
75#define MENELAUS_DETECT_CTRL 0x16
76#define MENELAUS_INT_MASK1 0x17
77#define MENELAUS_INT_MASK2 0x18
78#define MENELAUS_INT_STATUS1 0x19
79#define MENELAUS_INT_STATUS2 0x1A
80#define MENELAUS_INT_ACK1 0x1B
81#define MENELAUS_INT_ACK2 0x1C
82#define MENELAUS_GPIO_CTRL 0x1D
83#define MENELAUS_GPIO_IN 0x1E
84#define MENELAUS_GPIO_OUT 0x1F
85#define MENELAUS_BBSMS 0x20
86#define MENELAUS_RTC_CTRL 0x21
87#define MENELAUS_RTC_UPDATE 0x22
88#define MENELAUS_RTC_SEC 0x23
89#define MENELAUS_RTC_MIN 0x24
90#define MENELAUS_RTC_HR 0x25
91#define MENELAUS_RTC_DAY 0x26
92#define MENELAUS_RTC_MON 0x27
93#define MENELAUS_RTC_YR 0x28
94#define MENELAUS_RTC_WKDAY 0x29
95#define MENELAUS_RTC_AL_SEC 0x2A
96#define MENELAUS_RTC_AL_MIN 0x2B
97#define MENELAUS_RTC_AL_HR 0x2C
98#define MENELAUS_RTC_AL_DAY 0x2D
99#define MENELAUS_RTC_AL_MON 0x2E
100#define MENELAUS_RTC_AL_YR 0x2F
101#define MENELAUS_RTC_COMP_MSB 0x30
102#define MENELAUS_RTC_COMP_LSB 0x31
103#define MENELAUS_S1_PULL_EN 0x32
104#define MENELAUS_S1_PULL_DIR 0x33
105#define MENELAUS_S2_PULL_EN 0x34
106#define MENELAUS_S2_PULL_DIR 0x35
107#define MENELAUS_MCT_CTRL1 0x36
108#define MENELAUS_MCT_CTRL2 0x37
109#define MENELAUS_MCT_CTRL3 0x38
110#define MENELAUS_MCT_PIN_ST 0x39
111#define MENELAUS_DEBOUNCE1 0x3A
112
113#define IH_MENELAUS_IRQS 12
114#define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
115#define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
116#define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
117#define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
118#define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
119#define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
120#define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
121#define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
122#define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
123#define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
124#define MENELAUS_RTCERR_IRQ 10 /* RTC error */
125#define MENELAUS_PSHBTN_IRQ 11 /* Push button */
126#define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
127#define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
128#define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
129#define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
130
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300131/* VCORE_CTRL1 register */
132#define VCORE_CTRL1_BYP_COMP (1 << 5)
133#define VCORE_CTRL1_HW_NSW (1 << 7)
134
135/* GPIO_CTRL register */
136#define GPIO_CTRL_SLOTSELEN (1 << 5)
137#define GPIO_CTRL_SLPCTLEN (1 << 6)
138#define GPIO1_DIR_INPUT (1 << 0)
139#define GPIO2_DIR_INPUT (1 << 1)
140#define GPIO3_DIR_INPUT (1 << 2)
141
142/* MCT_CTRL1 register */
143#define MCT_CTRL1_S1_CMD_OD (1 << 2)
144#define MCT_CTRL1_S2_CMD_OD (1 << 3)
145
146/* MCT_CTRL2 register */
147#define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
148#define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
149#define MCT_CTRL2_S1CD_BUFEN (1 << 4)
150#define MCT_CTRL2_S2CD_BUFEN (1 << 5)
151#define MCT_CTRL2_S1CD_DBEN (1 << 6)
152#define MCT_CTRL2_S2CD_BEN (1 << 7)
153
154/* MCT_CTRL3 register */
155#define MCT_CTRL3_SLOT1_EN (1 << 0)
156#define MCT_CTRL3_SLOT2_EN (1 << 1)
157#define MCT_CTRL3_S1_AUTO_EN (1 << 2)
158#define MCT_CTRL3_S2_AUTO_EN (1 << 3)
159
160/* MCT_PIN_ST register */
161#define MCT_PIN_ST_S1_CD_ST (1 << 0)
162#define MCT_PIN_ST_S2_CD_ST (1 << 1)
163
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700164static void menelaus_work(struct work_struct *_menelaus);
165
166struct menelaus_chip {
167 struct mutex lock;
168 struct i2c_client *client;
169 struct work_struct work;
170#ifdef CONFIG_RTC_DRV_TWL92330
171 struct rtc_device *rtc;
172 u8 rtc_control;
173 unsigned uie:1;
174#endif
175 unsigned vcore_hw_mode:1;
176 u8 mask1, mask2;
177 void (*handlers[16])(struct menelaus_chip *);
178 void (*mmc_callback)(void *data, u8 mask);
179 void *mmc_callback_data;
180};
181
182static struct menelaus_chip *the_menelaus;
183
184static int menelaus_write_reg(int reg, u8 value)
185{
186 int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
187
188 if (val < 0) {
Emil Medve1f7c8232007-10-16 23:29:48 -0700189 pr_err(DRIVER_NAME ": write error");
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700190 return val;
191 }
192
193 return 0;
194}
195
196static int menelaus_read_reg(int reg)
197{
198 int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
199
200 if (val < 0)
Emil Medve1f7c8232007-10-16 23:29:48 -0700201 pr_err(DRIVER_NAME ": read error");
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700202
203 return val;
204}
205
206static int menelaus_enable_irq(int irq)
207{
208 if (irq > 7) {
209 irq -= 8;
210 the_menelaus->mask2 &= ~(1 << irq);
211 return menelaus_write_reg(MENELAUS_INT_MASK2,
212 the_menelaus->mask2);
213 } else {
214 the_menelaus->mask1 &= ~(1 << irq);
215 return menelaus_write_reg(MENELAUS_INT_MASK1,
216 the_menelaus->mask1);
217 }
218}
219
220static int menelaus_disable_irq(int irq)
221{
222 if (irq > 7) {
223 irq -= 8;
224 the_menelaus->mask2 |= (1 << irq);
225 return menelaus_write_reg(MENELAUS_INT_MASK2,
226 the_menelaus->mask2);
227 } else {
228 the_menelaus->mask1 |= (1 << irq);
229 return menelaus_write_reg(MENELAUS_INT_MASK1,
230 the_menelaus->mask1);
231 }
232}
233
234static int menelaus_ack_irq(int irq)
235{
236 if (irq > 7)
237 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
238 else
239 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
240}
241
242/* Adds a handler for an interrupt. Does not run in interrupt context */
243static int menelaus_add_irq_work(int irq,
244 void (*handler)(struct menelaus_chip *))
245{
246 int ret = 0;
247
248 mutex_lock(&the_menelaus->lock);
249 the_menelaus->handlers[irq] = handler;
250 ret = menelaus_enable_irq(irq);
251 mutex_unlock(&the_menelaus->lock);
252
253 return ret;
254}
255
256/* Removes handler for an interrupt */
257static int menelaus_remove_irq_work(int irq)
258{
259 int ret = 0;
260
261 mutex_lock(&the_menelaus->lock);
262 ret = menelaus_disable_irq(irq);
263 the_menelaus->handlers[irq] = NULL;
264 mutex_unlock(&the_menelaus->lock);
265
266 return ret;
267}
268
269/*
270 * Gets scheduled when a card detect interrupt happens. Note that in some cases
271 * this line is wired to card cover switch rather than the card detect switch
272 * in each slot. In this case the cards are not seen by menelaus.
273 * FIXME: Add handling for D1 too
274 */
275static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
276{
277 int reg;
278 unsigned char card_mask = 0;
279
280 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
281 if (reg < 0)
282 return;
283
284 if (!(reg & 0x1))
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300285 card_mask |= MCT_PIN_ST_S1_CD_ST;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700286
287 if (!(reg & 0x2))
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300288 card_mask |= MCT_PIN_ST_S2_CD_ST;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700289
290 if (menelaus_hw->mmc_callback)
291 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
292 card_mask);
293}
294
295/*
296 * Toggles the MMC slots between open-drain and push-pull mode.
297 */
298int menelaus_set_mmc_opendrain(int slot, int enable)
299{
300 int ret, val;
301
302 if (slot != 1 && slot != 2)
303 return -EINVAL;
304 mutex_lock(&the_menelaus->lock);
305 ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
306 if (ret < 0) {
307 mutex_unlock(&the_menelaus->lock);
308 return ret;
309 }
310 val = ret;
311 if (slot == 1) {
312 if (enable)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300313 val |= MCT_CTRL1_S1_CMD_OD;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700314 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300315 val &= ~MCT_CTRL1_S1_CMD_OD;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700316 } else {
317 if (enable)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300318 val |= MCT_CTRL1_S2_CMD_OD;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700319 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300320 val &= ~MCT_CTRL1_S2_CMD_OD;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700321 }
322 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
323 mutex_unlock(&the_menelaus->lock);
324
325 return ret;
326}
327EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
328
329int menelaus_set_slot_sel(int enable)
330{
331 int ret;
332
333 mutex_lock(&the_menelaus->lock);
334 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
335 if (ret < 0)
336 goto out;
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300337 ret |= GPIO2_DIR_INPUT;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700338 if (enable)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300339 ret |= GPIO_CTRL_SLOTSELEN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700340 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300341 ret &= ~GPIO_CTRL_SLOTSELEN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700342 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
343out:
344 mutex_unlock(&the_menelaus->lock);
345 return ret;
346}
347EXPORT_SYMBOL(menelaus_set_slot_sel);
348
349int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
350{
351 int ret, val;
352
353 if (slot != 1 && slot != 2)
354 return -EINVAL;
355 if (power >= 3)
356 return -EINVAL;
357
358 mutex_lock(&the_menelaus->lock);
359
360 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
361 if (ret < 0)
362 goto out;
363 val = ret;
364 if (slot == 1) {
365 if (cd_en)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300366 val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700367 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300368 val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700369 } else {
370 if (cd_en)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300371 val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700372 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300373 val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700374 }
375 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
376 if (ret < 0)
377 goto out;
378
379 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
380 if (ret < 0)
381 goto out;
382 val = ret;
383 if (slot == 1) {
384 if (enable)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300385 val |= MCT_CTRL3_SLOT1_EN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700386 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300387 val &= ~MCT_CTRL3_SLOT1_EN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700388 } else {
389 int b;
390
391 if (enable)
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300392 val |= MCT_CTRL3_SLOT2_EN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700393 else
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300394 val &= ~MCT_CTRL3_SLOT2_EN;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700395 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300396 b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700397 b |= power;
398 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
399 if (ret < 0)
400 goto out;
401 }
402 /* Disable autonomous shutdown */
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300403 val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700404 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
405out:
406 mutex_unlock(&the_menelaus->lock);
407 return ret;
408}
409EXPORT_SYMBOL(menelaus_set_mmc_slot);
410
411int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
412 void *data)
413{
414 int ret = 0;
415
416 the_menelaus->mmc_callback_data = data;
417 the_menelaus->mmc_callback = callback;
418 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
419 menelaus_mmc_cd_work);
420 if (ret < 0)
421 return ret;
422 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
423 menelaus_mmc_cd_work);
424 if (ret < 0)
425 return ret;
426 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
427 menelaus_mmc_cd_work);
428 if (ret < 0)
429 return ret;
430 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
431 menelaus_mmc_cd_work);
432
433 return ret;
434}
435EXPORT_SYMBOL(menelaus_register_mmc_callback);
436
437void menelaus_unregister_mmc_callback(void)
438{
439 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
440 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
441 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
442 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
443
444 the_menelaus->mmc_callback = NULL;
Jingoo Han59a9f7a2013-08-02 15:01:01 +0900445 the_menelaus->mmc_callback_data = NULL;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700446}
447EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
448
449struct menelaus_vtg {
450 const char *name;
451 u8 vtg_reg;
452 u8 vtg_shift;
453 u8 vtg_bits;
454 u8 mode_reg;
455};
456
457struct menelaus_vtg_value {
458 u16 vtg;
459 u16 val;
460};
461
462static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
463 int vtg_val, int mode)
464{
465 int val, ret;
466 struct i2c_client *c = the_menelaus->client;
467
468 mutex_lock(&the_menelaus->lock);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700469
470 ret = menelaus_read_reg(vtg->vtg_reg);
471 if (ret < 0)
472 goto out;
473 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
474 val |= vtg_val << vtg->vtg_shift;
475
476 dev_dbg(&c->dev, "Setting voltage '%s'"
477 "to %d mV (reg 0x%02x, val 0x%02x)\n",
478 vtg->name, mV, vtg->vtg_reg, val);
479
480 ret = menelaus_write_reg(vtg->vtg_reg, val);
481 if (ret < 0)
482 goto out;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700483 ret = menelaus_write_reg(vtg->mode_reg, mode);
484out:
485 mutex_unlock(&the_menelaus->lock);
486 if (ret == 0) {
487 /* Wait for voltage to stabilize */
488 msleep(1);
489 }
490 return ret;
491}
492
493static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
494 int n)
495{
496 int i;
497
498 for (i = 0; i < n; i++, tbl++)
499 if (tbl->vtg == vtg)
500 return tbl->val;
501 return -EINVAL;
502}
503
504/*
505 * Vcore can be programmed in two ways:
506 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
507 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
508 * and VCORE_CTRL4
509 *
510 * Call correct 'set' function accordingly
511 */
512
513static const struct menelaus_vtg_value vcore_values[] = {
514 { 1000, 0 },
515 { 1025, 1 },
516 { 1050, 2 },
517 { 1075, 3 },
518 { 1100, 4 },
519 { 1125, 5 },
520 { 1150, 6 },
521 { 1175, 7 },
522 { 1200, 8 },
523 { 1225, 9 },
524 { 1250, 10 },
525 { 1275, 11 },
526 { 1300, 12 },
527 { 1325, 13 },
528 { 1350, 14 },
529 { 1375, 15 },
530 { 1400, 16 },
531 { 1425, 17 },
532 { 1450, 18 },
533};
534
535int menelaus_set_vcore_sw(unsigned int mV)
536{
537 int val, ret;
538 struct i2c_client *c = the_menelaus->client;
539
540 val = menelaus_get_vtg_value(mV, vcore_values,
541 ARRAY_SIZE(vcore_values));
542 if (val < 0)
543 return -EINVAL;
544
545 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
546
547 /* Set SW mode and the voltage in one go. */
548 mutex_lock(&the_menelaus->lock);
549 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
550 if (ret == 0)
551 the_menelaus->vcore_hw_mode = 0;
552 mutex_unlock(&the_menelaus->lock);
553 msleep(1);
554
555 return ret;
556}
557
558int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
559{
560 int fval, rval, val, ret;
561 struct i2c_client *c = the_menelaus->client;
562
563 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
564 ARRAY_SIZE(vcore_values));
565 if (rval < 0)
566 return -EINVAL;
567 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
568 ARRAY_SIZE(vcore_values));
569 if (fval < 0)
570 return -EINVAL;
571
572 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
573 floor_mV, roof_mV);
574
575 mutex_lock(&the_menelaus->lock);
576 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
577 if (ret < 0)
578 goto out;
579 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
580 if (ret < 0)
581 goto out;
582 if (!the_menelaus->vcore_hw_mode) {
583 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
584 /* HW mode, turn OFF byte comparator */
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300585 val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700586 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
587 the_menelaus->vcore_hw_mode = 1;
588 }
589 msleep(1);
590out:
591 mutex_unlock(&the_menelaus->lock);
592 return ret;
593}
594
595static const struct menelaus_vtg vmem_vtg = {
596 .name = "VMEM",
597 .vtg_reg = MENELAUS_LDO_CTRL1,
598 .vtg_shift = 0,
599 .vtg_bits = 2,
600 .mode_reg = MENELAUS_LDO_CTRL3,
601};
602
603static const struct menelaus_vtg_value vmem_values[] = {
604 { 1500, 0 },
605 { 1800, 1 },
606 { 1900, 2 },
607 { 2500, 3 },
608};
609
610int menelaus_set_vmem(unsigned int mV)
611{
612 int val;
613
614 if (mV == 0)
615 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
616
617 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
618 if (val < 0)
619 return -EINVAL;
620 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
621}
622EXPORT_SYMBOL(menelaus_set_vmem);
623
624static const struct menelaus_vtg vio_vtg = {
625 .name = "VIO",
626 .vtg_reg = MENELAUS_LDO_CTRL1,
627 .vtg_shift = 2,
628 .vtg_bits = 2,
629 .mode_reg = MENELAUS_LDO_CTRL4,
630};
631
632static const struct menelaus_vtg_value vio_values[] = {
633 { 1500, 0 },
634 { 1800, 1 },
635 { 2500, 2 },
636 { 2800, 3 },
637};
638
639int menelaus_set_vio(unsigned int mV)
640{
641 int val;
642
643 if (mV == 0)
644 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
645
646 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
647 if (val < 0)
648 return -EINVAL;
649 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
650}
651EXPORT_SYMBOL(menelaus_set_vio);
652
653static const struct menelaus_vtg_value vdcdc_values[] = {
654 { 1500, 0 },
655 { 1800, 1 },
656 { 2000, 2 },
657 { 2200, 3 },
658 { 2400, 4 },
659 { 2800, 5 },
660 { 3000, 6 },
661 { 3300, 7 },
662};
663
664static const struct menelaus_vtg vdcdc2_vtg = {
665 .name = "VDCDC2",
666 .vtg_reg = MENELAUS_DCDC_CTRL1,
667 .vtg_shift = 0,
668 .vtg_bits = 3,
669 .mode_reg = MENELAUS_DCDC_CTRL2,
670};
671
672static const struct menelaus_vtg vdcdc3_vtg = {
673 .name = "VDCDC3",
674 .vtg_reg = MENELAUS_DCDC_CTRL1,
675 .vtg_shift = 3,
676 .vtg_bits = 3,
677 .mode_reg = MENELAUS_DCDC_CTRL3,
678};
679
680int menelaus_set_vdcdc(int dcdc, unsigned int mV)
681{
682 const struct menelaus_vtg *vtg;
683 int val;
684
685 if (dcdc != 2 && dcdc != 3)
686 return -EINVAL;
687 if (dcdc == 2)
688 vtg = &vdcdc2_vtg;
689 else
690 vtg = &vdcdc3_vtg;
691
692 if (mV == 0)
693 return menelaus_set_voltage(vtg, 0, 0, 0);
694
695 val = menelaus_get_vtg_value(mV, vdcdc_values,
696 ARRAY_SIZE(vdcdc_values));
697 if (val < 0)
698 return -EINVAL;
699 return menelaus_set_voltage(vtg, mV, val, 0x03);
700}
701
702static const struct menelaus_vtg_value vmmc_values[] = {
703 { 1850, 0 },
704 { 2800, 1 },
705 { 3000, 2 },
706 { 3100, 3 },
707};
708
709static const struct menelaus_vtg vmmc_vtg = {
710 .name = "VMMC",
711 .vtg_reg = MENELAUS_LDO_CTRL1,
712 .vtg_shift = 6,
713 .vtg_bits = 2,
714 .mode_reg = MENELAUS_LDO_CTRL7,
715};
716
717int menelaus_set_vmmc(unsigned int mV)
718{
719 int val;
720
721 if (mV == 0)
722 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
723
724 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
725 if (val < 0)
726 return -EINVAL;
727 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
728}
729EXPORT_SYMBOL(menelaus_set_vmmc);
730
731
732static const struct menelaus_vtg_value vaux_values[] = {
733 { 1500, 0 },
734 { 1800, 1 },
735 { 2500, 2 },
736 { 2800, 3 },
737};
738
739static const struct menelaus_vtg vaux_vtg = {
740 .name = "VAUX",
741 .vtg_reg = MENELAUS_LDO_CTRL1,
742 .vtg_shift = 4,
743 .vtg_bits = 2,
744 .mode_reg = MENELAUS_LDO_CTRL6,
745};
746
747int menelaus_set_vaux(unsigned int mV)
748{
749 int val;
750
751 if (mV == 0)
752 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
753
754 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
755 if (val < 0)
756 return -EINVAL;
757 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
758}
759EXPORT_SYMBOL(menelaus_set_vaux);
760
761int menelaus_get_slot_pin_states(void)
762{
763 return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
764}
765EXPORT_SYMBOL(menelaus_get_slot_pin_states);
766
767int menelaus_set_regulator_sleep(int enable, u32 val)
768{
769 int t, ret;
770 struct i2c_client *c = the_menelaus->client;
771
772 mutex_lock(&the_menelaus->lock);
773 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
774 if (ret < 0)
775 goto out;
776
777 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
778
779 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
780 if (ret < 0)
781 goto out;
Jarkko Nikula1c888e22010-08-08 20:05:24 +0300782 t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700783 if (enable)
784 ret |= t;
785 else
786 ret &= ~t;
787 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
788out:
789 mutex_unlock(&the_menelaus->lock);
790 return ret;
791}
792
793/*-----------------------------------------------------------------------*/
794
795/* Handles Menelaus interrupts. Does not run in interrupt context */
796static void menelaus_work(struct work_struct *_menelaus)
797{
798 struct menelaus_chip *menelaus =
799 container_of(_menelaus, struct menelaus_chip, work);
800 void (*handler)(struct menelaus_chip *menelaus);
801
802 while (1) {
803 unsigned isr;
804
805 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
806 & ~menelaus->mask2) << 8;
807 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
808 & ~menelaus->mask1;
809 if (!isr)
810 break;
811
812 while (isr) {
813 int irq = fls(isr) - 1;
814 isr &= ~(1 << irq);
815
816 mutex_lock(&menelaus->lock);
817 menelaus_disable_irq(irq);
818 menelaus_ack_irq(irq);
819 handler = menelaus->handlers[irq];
820 if (handler)
821 handler(menelaus);
822 menelaus_enable_irq(irq);
823 mutex_unlock(&menelaus->lock);
824 }
825 }
826 enable_irq(menelaus->client->irq);
827}
828
829/*
830 * We cannot use I2C in interrupt context, so we just schedule work.
831 */
832static irqreturn_t menelaus_irq(int irq, void *_menelaus)
833{
834 struct menelaus_chip *menelaus = _menelaus;
835
836 disable_irq_nosync(irq);
837 (void)schedule_work(&menelaus->work);
838
839 return IRQ_HANDLED;
840}
841
842/*-----------------------------------------------------------------------*/
843
844/*
845 * The RTC needs to be set once, then it runs on backup battery power.
846 * It supports alarms, including system wake alarms (from some modes);
847 * and 1/second IRQs if requested.
848 */
849#ifdef CONFIG_RTC_DRV_TWL92330
850
851#define RTC_CTRL_RTC_EN (1 << 0)
852#define RTC_CTRL_AL_EN (1 << 1)
853#define RTC_CTRL_MODE12 (1 << 2)
854#define RTC_CTRL_EVERY_MASK (3 << 3)
855#define RTC_CTRL_EVERY_SEC (0 << 3)
856#define RTC_CTRL_EVERY_MIN (1 << 3)
857#define RTC_CTRL_EVERY_HR (2 << 3)
858#define RTC_CTRL_EVERY_DAY (3 << 3)
859
860#define RTC_UPDATE_EVERY 0x08
861
862#define RTC_HR_PM (1 << 7)
863
864static void menelaus_to_time(char *regs, struct rtc_time *t)
865{
Adrian Bunke4d33962008-10-18 20:28:46 -0700866 t->tm_sec = bcd2bin(regs[0]);
867 t->tm_min = bcd2bin(regs[1]);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700868 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
Adrian Bunke4d33962008-10-18 20:28:46 -0700869 t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700870 if (regs[2] & RTC_HR_PM)
871 t->tm_hour += 12;
872 } else
Adrian Bunke4d33962008-10-18 20:28:46 -0700873 t->tm_hour = bcd2bin(regs[2] & 0x3f);
874 t->tm_mday = bcd2bin(regs[3]);
875 t->tm_mon = bcd2bin(regs[4]) - 1;
876 t->tm_year = bcd2bin(regs[5]) + 100;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700877}
878
879static int time_to_menelaus(struct rtc_time *t, int regnum)
880{
881 int hour, status;
882
Adrian Bunke4d33962008-10-18 20:28:46 -0700883 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700884 if (status < 0)
885 goto fail;
886
Adrian Bunke4d33962008-10-18 20:28:46 -0700887 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700888 if (status < 0)
889 goto fail;
890
891 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
892 hour = t->tm_hour + 1;
893 if (hour > 12)
Adrian Bunke4d33962008-10-18 20:28:46 -0700894 hour = RTC_HR_PM | bin2bcd(hour - 12);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700895 else
Adrian Bunke4d33962008-10-18 20:28:46 -0700896 hour = bin2bcd(hour);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700897 } else
Adrian Bunke4d33962008-10-18 20:28:46 -0700898 hour = bin2bcd(t->tm_hour);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700899 status = menelaus_write_reg(regnum++, hour);
900 if (status < 0)
901 goto fail;
902
Adrian Bunke4d33962008-10-18 20:28:46 -0700903 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700904 if (status < 0)
905 goto fail;
906
Adrian Bunke4d33962008-10-18 20:28:46 -0700907 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700908 if (status < 0)
909 goto fail;
910
Adrian Bunke4d33962008-10-18 20:28:46 -0700911 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700912 if (status < 0)
913 goto fail;
914
915 return 0;
916fail:
917 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
918 --regnum, status);
919 return status;
920}
921
922static int menelaus_read_time(struct device *dev, struct rtc_time *t)
923{
924 struct i2c_msg msg[2];
925 char regs[7];
926 int status;
927
928 /* block read date and time registers */
929 regs[0] = MENELAUS_RTC_SEC;
930
931 msg[0].addr = MENELAUS_I2C_ADDRESS;
932 msg[0].flags = 0;
933 msg[0].len = 1;
934 msg[0].buf = regs;
935
936 msg[1].addr = MENELAUS_I2C_ADDRESS;
937 msg[1].flags = I2C_M_RD;
938 msg[1].len = sizeof(regs);
939 msg[1].buf = regs;
940
941 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
942 if (status != 2) {
943 dev_err(dev, "%s error %d\n", "read", status);
944 return -EIO;
945 }
946
947 menelaus_to_time(regs, t);
Adrian Bunke4d33962008-10-18 20:28:46 -0700948 t->tm_wday = bcd2bin(regs[6]);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700949
950 return 0;
951}
952
953static int menelaus_set_time(struct device *dev, struct rtc_time *t)
954{
955 int status;
956
957 /* write date and time registers */
958 status = time_to_menelaus(t, MENELAUS_RTC_SEC);
959 if (status < 0)
960 return status;
Adrian Bunke4d33962008-10-18 20:28:46 -0700961 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday));
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700962 if (status < 0) {
David Brownellc1147cc2007-08-14 18:37:14 +0200963 dev_err(&the_menelaus->client->dev, "rtc write reg %02x "
Tony Lindgren0c4a59f2007-07-17 04:06:09 -0700964 "err %d\n", MENELAUS_RTC_WKDAY, status);
965 return status;
966 }
967
968 /* now commit the write */
969 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
970 if (status < 0)
971 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
972 status);
973
974 return 0;
975}
976
977static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
978{
979 struct i2c_msg msg[2];
980 char regs[6];
981 int status;
982
983 /* block read alarm registers */
984 regs[0] = MENELAUS_RTC_AL_SEC;
985
986 msg[0].addr = MENELAUS_I2C_ADDRESS;
987 msg[0].flags = 0;
988 msg[0].len = 1;
989 msg[0].buf = regs;
990
991 msg[1].addr = MENELAUS_I2C_ADDRESS;
992 msg[1].flags = I2C_M_RD;
993 msg[1].len = sizeof(regs);
994 msg[1].buf = regs;
995
996 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
997 if (status != 2) {
998 dev_err(dev, "%s error %d\n", "alarm read", status);
999 return -EIO;
1000 }
1001
1002 menelaus_to_time(regs, &w->time);
1003
1004 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
1005
1006 /* NOTE we *could* check if actually pending... */
1007 w->pending = 0;
1008
1009 return 0;
1010}
1011
1012static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
1013{
1014 int status;
1015
1016 if (the_menelaus->client->irq <= 0 && w->enabled)
1017 return -ENODEV;
1018
1019 /* clear previous alarm enable */
1020 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
1021 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1022 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1023 the_menelaus->rtc_control);
1024 if (status < 0)
1025 return status;
1026 }
1027
1028 /* write alarm registers */
1029 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
1030 if (status < 0)
1031 return status;
1032
1033 /* enable alarm if requested */
1034 if (w->enabled) {
1035 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1036 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1037 the_menelaus->rtc_control);
1038 }
1039
1040 return status;
1041}
1042
1043#ifdef CONFIG_RTC_INTF_DEV
1044
1045static void menelaus_rtc_update_work(struct menelaus_chip *m)
1046{
1047 /* report 1/sec update */
1048 local_irq_disable();
1049 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
1050 local_irq_enable();
1051}
1052
1053static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1054{
1055 int status;
1056
1057 if (the_menelaus->client->irq <= 0)
1058 return -ENOIOCTLCMD;
1059
1060 switch (cmd) {
1061 /* alarm IRQ */
1062 case RTC_AIE_ON:
1063 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1064 return 0;
1065 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1066 break;
1067 case RTC_AIE_OFF:
1068 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1069 return 0;
1070 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1071 break;
1072 /* 1/second "update" IRQ */
1073 case RTC_UIE_ON:
1074 if (the_menelaus->uie)
1075 return 0;
1076 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1077 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1078 menelaus_rtc_update_work);
1079 if (status == 0)
1080 the_menelaus->uie = 1;
1081 return status;
1082 case RTC_UIE_OFF:
1083 if (!the_menelaus->uie)
1084 return 0;
1085 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1086 if (status == 0)
1087 the_menelaus->uie = 0;
1088 return status;
1089 default:
1090 return -ENOIOCTLCMD;
1091 }
1092 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1093}
1094
1095#else
1096#define menelaus_ioctl NULL
1097#endif
1098
1099/* REVISIT no compensation register support ... */
1100
1101static const struct rtc_class_ops menelaus_rtc_ops = {
1102 .ioctl = menelaus_ioctl,
1103 .read_time = menelaus_read_time,
1104 .set_time = menelaus_set_time,
1105 .read_alarm = menelaus_read_alarm,
1106 .set_alarm = menelaus_set_alarm,
1107};
1108
1109static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1110{
1111 /* report alarm */
1112 local_irq_disable();
1113 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1114 local_irq_enable();
1115
1116 /* then disable it; alarms are oneshot */
1117 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1118 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1119}
1120
1121static inline void menelaus_rtc_init(struct menelaus_chip *m)
1122{
1123 int alarm = (m->client->irq > 0);
1124
1125 /* assume 32KDETEN pin is pulled high */
1126 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1127 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1128 return;
1129 }
1130
1131 /* support RTC alarm; it can issue wakeups */
1132 if (alarm) {
1133 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1134 menelaus_rtc_alarm_work) < 0) {
1135 dev_err(&m->client->dev, "can't handle RTC alarm\n");
1136 return;
1137 }
1138 device_init_wakeup(&m->client->dev, 1);
1139 }
1140
1141 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1142 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1143 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1144 || (m->rtc_control & RTC_CTRL_AL_EN)
1145 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1146 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1147 dev_warn(&m->client->dev, "rtc clock needs setting\n");
1148 m->rtc_control |= RTC_CTRL_RTC_EN;
1149 }
1150 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1151 m->rtc_control &= ~RTC_CTRL_AL_EN;
1152 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1153 }
1154
1155 m->rtc = rtc_device_register(DRIVER_NAME,
1156 &m->client->dev,
1157 &menelaus_rtc_ops, THIS_MODULE);
1158 if (IS_ERR(m->rtc)) {
1159 if (alarm) {
1160 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1161 device_init_wakeup(&m->client->dev, 0);
1162 }
1163 dev_err(&m->client->dev, "can't register RTC: %d\n",
1164 (int) PTR_ERR(m->rtc));
1165 the_menelaus->rtc = NULL;
1166 }
1167}
1168
1169#else
1170
1171static inline void menelaus_rtc_init(struct menelaus_chip *m)
1172{
1173 /* nothing */
1174}
1175
1176#endif
1177
1178/*-----------------------------------------------------------------------*/
1179
1180static struct i2c_driver menelaus_i2c_driver;
1181
Jean Delvared2653e92008-04-29 23:11:39 +02001182static int menelaus_probe(struct i2c_client *client,
1183 const struct i2c_device_id *id)
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001184{
1185 struct menelaus_chip *menelaus;
Julia Lawall42a71ef2014-08-21 07:06:06 -05001186 int rev = 0;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001187 int err = 0;
1188 struct menelaus_platform_data *menelaus_pdata =
Jingoo Han334a41c2013-07-30 17:10:05 +09001189 dev_get_platdata(&client->dev);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001190
1191 if (the_menelaus) {
1192 dev_dbg(&client->dev, "only one %s for now\n",
1193 DRIVER_NAME);
1194 return -ENODEV;
1195 }
1196
Jingoo Han7a404312013-08-20 16:05:11 +09001197 menelaus = devm_kzalloc(&client->dev, sizeof(*menelaus), GFP_KERNEL);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001198 if (!menelaus)
1199 return -ENOMEM;
1200
1201 i2c_set_clientdata(client, menelaus);
1202
1203 the_menelaus = menelaus;
1204 menelaus->client = client;
1205
1206 /* If a true probe check the device */
1207 rev = menelaus_read_reg(MENELAUS_REV);
1208 if (rev < 0) {
Emil Medve1f7c8232007-10-16 23:29:48 -07001209 pr_err(DRIVER_NAME ": device not found");
Jingoo Han7a404312013-08-20 16:05:11 +09001210 return -ENODEV;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001211 }
1212
1213 /* Ack and disable all Menelaus interrupts */
1214 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1215 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1216 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1217 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1218 menelaus->mask1 = 0xff;
1219 menelaus->mask2 = 0xff;
1220
1221 /* Set output buffer strengths */
1222 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1223
1224 if (client->irq > 0) {
Yong Zhangf742b96e2011-09-15 21:52:09 +02001225 err = request_irq(client->irq, menelaus_irq, 0,
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001226 DRIVER_NAME, menelaus);
1227 if (err) {
Joe Perches898eb712007-10-18 03:06:30 -07001228 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001229 client->irq, err);
Jingoo Han7a404312013-08-20 16:05:11 +09001230 return err;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001231 }
1232 }
1233
1234 mutex_init(&menelaus->lock);
1235 INIT_WORK(&menelaus->work, menelaus_work);
1236
1237 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1238
Julia Lawall42a71ef2014-08-21 07:06:06 -05001239 err = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1240 if (err < 0)
Jingoo Han7a404312013-08-20 16:05:11 +09001241 goto fail;
Julia Lawall42a71ef2014-08-21 07:06:06 -05001242 if (err & BIT(7))
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001243 menelaus->vcore_hw_mode = 1;
1244 else
1245 menelaus->vcore_hw_mode = 0;
1246
1247 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1248 err = menelaus_pdata->late_init(&client->dev);
1249 if (err < 0)
Jingoo Han7a404312013-08-20 16:05:11 +09001250 goto fail;
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001251 }
1252
1253 menelaus_rtc_init(menelaus);
1254
1255 return 0;
Jingoo Han7a404312013-08-20 16:05:11 +09001256fail:
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001257 free_irq(client->irq, menelaus);
Tejun Heo43829732012-08-20 14:51:24 -07001258 flush_work(&menelaus->work);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001259 return err;
1260}
1261
1262static int __exit menelaus_remove(struct i2c_client *client)
1263{
1264 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1265
1266 free_irq(client->irq, menelaus);
Tejun Heo43829732012-08-20 14:51:24 -07001267 flush_work(&menelaus->work);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001268 the_menelaus = NULL;
1269 return 0;
1270}
1271
Jean Delvare3760f732008-04-29 23:11:40 +02001272static const struct i2c_device_id menelaus_id[] = {
1273 { "menelaus", 0 },
1274 { }
1275};
1276MODULE_DEVICE_TABLE(i2c, menelaus_id);
1277
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001278static struct i2c_driver menelaus_i2c_driver = {
1279 .driver = {
1280 .name = DRIVER_NAME,
1281 },
1282 .probe = menelaus_probe,
1283 .remove = __exit_p(menelaus_remove),
Jean Delvare3760f732008-04-29 23:11:40 +02001284 .id_table = menelaus_id,
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001285};
1286
Sachin Kamat1d3c7f52014-05-29 17:13:22 +05301287module_i2c_driver(menelaus_i2c_driver);
Tony Lindgren0c4a59f2007-07-17 04:06:09 -07001288
1289MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1290MODULE_DESCRIPTION("I2C interface for Menelaus.");
1291MODULE_LICENSE("GPL");