Commit | Line | Data |
---|---|---|
0c4a59fe TL |
1 | /* |
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> | |
43 | ||
44 | #include <asm/mach-types.h> | |
45 | #include <asm/mach/irq.h> | |
46 | ||
47 | #include <asm/arch/gpio.h> | |
48 | #include <asm/arch/menelaus.h> | |
49 | ||
50 | #define DRIVER_NAME "menelaus" | |
51 | ||
52 | #define pr_err(fmt, arg...) printk(KERN_ERR DRIVER_NAME ": ", ## arg); | |
53 | ||
54 | #define MENELAUS_I2C_ADDRESS 0x72 | |
55 | ||
56 | #define MENELAUS_REV 0x01 | |
57 | #define MENELAUS_VCORE_CTRL1 0x02 | |
58 | #define MENELAUS_VCORE_CTRL2 0x03 | |
59 | #define MENELAUS_VCORE_CTRL3 0x04 | |
60 | #define MENELAUS_VCORE_CTRL4 0x05 | |
61 | #define MENELAUS_VCORE_CTRL5 0x06 | |
62 | #define MENELAUS_DCDC_CTRL1 0x07 | |
63 | #define MENELAUS_DCDC_CTRL2 0x08 | |
64 | #define MENELAUS_DCDC_CTRL3 0x09 | |
65 | #define MENELAUS_LDO_CTRL1 0x0A | |
66 | #define MENELAUS_LDO_CTRL2 0x0B | |
67 | #define MENELAUS_LDO_CTRL3 0x0C | |
68 | #define MENELAUS_LDO_CTRL4 0x0D | |
69 | #define MENELAUS_LDO_CTRL5 0x0E | |
70 | #define MENELAUS_LDO_CTRL6 0x0F | |
71 | #define MENELAUS_LDO_CTRL7 0x10 | |
72 | #define MENELAUS_LDO_CTRL8 0x11 | |
73 | #define MENELAUS_SLEEP_CTRL1 0x12 | |
74 | #define MENELAUS_SLEEP_CTRL2 0x13 | |
75 | #define MENELAUS_DEVICE_OFF 0x14 | |
76 | #define MENELAUS_OSC_CTRL 0x15 | |
77 | #define MENELAUS_DETECT_CTRL 0x16 | |
78 | #define MENELAUS_INT_MASK1 0x17 | |
79 | #define MENELAUS_INT_MASK2 0x18 | |
80 | #define MENELAUS_INT_STATUS1 0x19 | |
81 | #define MENELAUS_INT_STATUS2 0x1A | |
82 | #define MENELAUS_INT_ACK1 0x1B | |
83 | #define MENELAUS_INT_ACK2 0x1C | |
84 | #define MENELAUS_GPIO_CTRL 0x1D | |
85 | #define MENELAUS_GPIO_IN 0x1E | |
86 | #define MENELAUS_GPIO_OUT 0x1F | |
87 | #define MENELAUS_BBSMS 0x20 | |
88 | #define MENELAUS_RTC_CTRL 0x21 | |
89 | #define MENELAUS_RTC_UPDATE 0x22 | |
90 | #define MENELAUS_RTC_SEC 0x23 | |
91 | #define MENELAUS_RTC_MIN 0x24 | |
92 | #define MENELAUS_RTC_HR 0x25 | |
93 | #define MENELAUS_RTC_DAY 0x26 | |
94 | #define MENELAUS_RTC_MON 0x27 | |
95 | #define MENELAUS_RTC_YR 0x28 | |
96 | #define MENELAUS_RTC_WKDAY 0x29 | |
97 | #define MENELAUS_RTC_AL_SEC 0x2A | |
98 | #define MENELAUS_RTC_AL_MIN 0x2B | |
99 | #define MENELAUS_RTC_AL_HR 0x2C | |
100 | #define MENELAUS_RTC_AL_DAY 0x2D | |
101 | #define MENELAUS_RTC_AL_MON 0x2E | |
102 | #define MENELAUS_RTC_AL_YR 0x2F | |
103 | #define MENELAUS_RTC_COMP_MSB 0x30 | |
104 | #define MENELAUS_RTC_COMP_LSB 0x31 | |
105 | #define MENELAUS_S1_PULL_EN 0x32 | |
106 | #define MENELAUS_S1_PULL_DIR 0x33 | |
107 | #define MENELAUS_S2_PULL_EN 0x34 | |
108 | #define MENELAUS_S2_PULL_DIR 0x35 | |
109 | #define MENELAUS_MCT_CTRL1 0x36 | |
110 | #define MENELAUS_MCT_CTRL2 0x37 | |
111 | #define MENELAUS_MCT_CTRL3 0x38 | |
112 | #define MENELAUS_MCT_PIN_ST 0x39 | |
113 | #define MENELAUS_DEBOUNCE1 0x3A | |
114 | ||
115 | #define IH_MENELAUS_IRQS 12 | |
116 | #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */ | |
117 | #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */ | |
118 | #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */ | |
119 | #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */ | |
120 | #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */ | |
121 | #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */ | |
122 | #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */ | |
123 | #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */ | |
124 | #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */ | |
125 | #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */ | |
126 | #define MENELAUS_RTCERR_IRQ 10 /* RTC error */ | |
127 | #define MENELAUS_PSHBTN_IRQ 11 /* Push button */ | |
128 | #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */ | |
129 | #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */ | |
130 | #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */ | |
131 | #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */ | |
132 | ||
133 | static void menelaus_work(struct work_struct *_menelaus); | |
134 | ||
135 | struct menelaus_chip { | |
136 | struct mutex lock; | |
137 | struct i2c_client *client; | |
138 | struct work_struct work; | |
139 | #ifdef CONFIG_RTC_DRV_TWL92330 | |
140 | struct rtc_device *rtc; | |
141 | u8 rtc_control; | |
142 | unsigned uie:1; | |
143 | #endif | |
144 | unsigned vcore_hw_mode:1; | |
145 | u8 mask1, mask2; | |
146 | void (*handlers[16])(struct menelaus_chip *); | |
147 | void (*mmc_callback)(void *data, u8 mask); | |
148 | void *mmc_callback_data; | |
149 | }; | |
150 | ||
151 | static struct menelaus_chip *the_menelaus; | |
152 | ||
153 | static int menelaus_write_reg(int reg, u8 value) | |
154 | { | |
155 | int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value); | |
156 | ||
157 | if (val < 0) { | |
158 | pr_err("write error"); | |
159 | return val; | |
160 | } | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | static int menelaus_read_reg(int reg) | |
166 | { | |
167 | int val = i2c_smbus_read_byte_data(the_menelaus->client, reg); | |
168 | ||
169 | if (val < 0) | |
170 | pr_err("read error"); | |
171 | ||
172 | return val; | |
173 | } | |
174 | ||
175 | static int menelaus_enable_irq(int irq) | |
176 | { | |
177 | if (irq > 7) { | |
178 | irq -= 8; | |
179 | the_menelaus->mask2 &= ~(1 << irq); | |
180 | return menelaus_write_reg(MENELAUS_INT_MASK2, | |
181 | the_menelaus->mask2); | |
182 | } else { | |
183 | the_menelaus->mask1 &= ~(1 << irq); | |
184 | return menelaus_write_reg(MENELAUS_INT_MASK1, | |
185 | the_menelaus->mask1); | |
186 | } | |
187 | } | |
188 | ||
189 | static int menelaus_disable_irq(int irq) | |
190 | { | |
191 | if (irq > 7) { | |
192 | irq -= 8; | |
193 | the_menelaus->mask2 |= (1 << irq); | |
194 | return menelaus_write_reg(MENELAUS_INT_MASK2, | |
195 | the_menelaus->mask2); | |
196 | } else { | |
197 | the_menelaus->mask1 |= (1 << irq); | |
198 | return menelaus_write_reg(MENELAUS_INT_MASK1, | |
199 | the_menelaus->mask1); | |
200 | } | |
201 | } | |
202 | ||
203 | static int menelaus_ack_irq(int irq) | |
204 | { | |
205 | if (irq > 7) | |
206 | return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8)); | |
207 | else | |
208 | return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq); | |
209 | } | |
210 | ||
211 | /* Adds a handler for an interrupt. Does not run in interrupt context */ | |
212 | static int menelaus_add_irq_work(int irq, | |
213 | void (*handler)(struct menelaus_chip *)) | |
214 | { | |
215 | int ret = 0; | |
216 | ||
217 | mutex_lock(&the_menelaus->lock); | |
218 | the_menelaus->handlers[irq] = handler; | |
219 | ret = menelaus_enable_irq(irq); | |
220 | mutex_unlock(&the_menelaus->lock); | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
225 | /* Removes handler for an interrupt */ | |
226 | static int menelaus_remove_irq_work(int irq) | |
227 | { | |
228 | int ret = 0; | |
229 | ||
230 | mutex_lock(&the_menelaus->lock); | |
231 | ret = menelaus_disable_irq(irq); | |
232 | the_menelaus->handlers[irq] = NULL; | |
233 | mutex_unlock(&the_menelaus->lock); | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Gets scheduled when a card detect interrupt happens. Note that in some cases | |
240 | * this line is wired to card cover switch rather than the card detect switch | |
241 | * in each slot. In this case the cards are not seen by menelaus. | |
242 | * FIXME: Add handling for D1 too | |
243 | */ | |
244 | static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw) | |
245 | { | |
246 | int reg; | |
247 | unsigned char card_mask = 0; | |
248 | ||
249 | reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST); | |
250 | if (reg < 0) | |
251 | return; | |
252 | ||
253 | if (!(reg & 0x1)) | |
254 | card_mask |= (1 << 0); | |
255 | ||
256 | if (!(reg & 0x2)) | |
257 | card_mask |= (1 << 1); | |
258 | ||
259 | if (menelaus_hw->mmc_callback) | |
260 | menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data, | |
261 | card_mask); | |
262 | } | |
263 | ||
264 | /* | |
265 | * Toggles the MMC slots between open-drain and push-pull mode. | |
266 | */ | |
267 | int menelaus_set_mmc_opendrain(int slot, int enable) | |
268 | { | |
269 | int ret, val; | |
270 | ||
271 | if (slot != 1 && slot != 2) | |
272 | return -EINVAL; | |
273 | mutex_lock(&the_menelaus->lock); | |
274 | ret = menelaus_read_reg(MENELAUS_MCT_CTRL1); | |
275 | if (ret < 0) { | |
276 | mutex_unlock(&the_menelaus->lock); | |
277 | return ret; | |
278 | } | |
279 | val = ret; | |
280 | if (slot == 1) { | |
281 | if (enable) | |
282 | val |= 1 << 2; | |
283 | else | |
284 | val &= ~(1 << 2); | |
285 | } else { | |
286 | if (enable) | |
287 | val |= 1 << 3; | |
288 | else | |
289 | val &= ~(1 << 3); | |
290 | } | |
291 | ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val); | |
292 | mutex_unlock(&the_menelaus->lock); | |
293 | ||
294 | return ret; | |
295 | } | |
296 | EXPORT_SYMBOL(menelaus_set_mmc_opendrain); | |
297 | ||
298 | int menelaus_set_slot_sel(int enable) | |
299 | { | |
300 | int ret; | |
301 | ||
302 | mutex_lock(&the_menelaus->lock); | |
303 | ret = menelaus_read_reg(MENELAUS_GPIO_CTRL); | |
304 | if (ret < 0) | |
305 | goto out; | |
306 | ret |= 0x02; | |
307 | if (enable) | |
308 | ret |= 1 << 5; | |
309 | else | |
310 | ret &= ~(1 << 5); | |
311 | ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret); | |
312 | out: | |
313 | mutex_unlock(&the_menelaus->lock); | |
314 | return ret; | |
315 | } | |
316 | EXPORT_SYMBOL(menelaus_set_slot_sel); | |
317 | ||
318 | int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en) | |
319 | { | |
320 | int ret, val; | |
321 | ||
322 | if (slot != 1 && slot != 2) | |
323 | return -EINVAL; | |
324 | if (power >= 3) | |
325 | return -EINVAL; | |
326 | ||
327 | mutex_lock(&the_menelaus->lock); | |
328 | ||
329 | ret = menelaus_read_reg(MENELAUS_MCT_CTRL2); | |
330 | if (ret < 0) | |
331 | goto out; | |
332 | val = ret; | |
333 | if (slot == 1) { | |
334 | if (cd_en) | |
335 | val |= (1 << 4) | (1 << 6); | |
336 | else | |
337 | val &= ~((1 << 4) | (1 << 6)); | |
338 | } else { | |
339 | if (cd_en) | |
340 | val |= (1 << 5) | (1 << 7); | |
341 | else | |
342 | val &= ~((1 << 5) | (1 << 7)); | |
343 | } | |
344 | ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val); | |
345 | if (ret < 0) | |
346 | goto out; | |
347 | ||
348 | ret = menelaus_read_reg(MENELAUS_MCT_CTRL3); | |
349 | if (ret < 0) | |
350 | goto out; | |
351 | val = ret; | |
352 | if (slot == 1) { | |
353 | if (enable) | |
354 | val |= 1 << 0; | |
355 | else | |
356 | val &= ~(1 << 0); | |
357 | } else { | |
358 | int b; | |
359 | ||
360 | if (enable) | |
361 | ret |= 1 << 1; | |
362 | else | |
363 | ret &= ~(1 << 1); | |
364 | b = menelaus_read_reg(MENELAUS_MCT_CTRL2); | |
365 | b &= ~0x03; | |
366 | b |= power; | |
367 | ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b); | |
368 | if (ret < 0) | |
369 | goto out; | |
370 | } | |
371 | /* Disable autonomous shutdown */ | |
372 | val &= ~(0x03 << 2); | |
373 | ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val); | |
374 | out: | |
375 | mutex_unlock(&the_menelaus->lock); | |
376 | return ret; | |
377 | } | |
378 | EXPORT_SYMBOL(menelaus_set_mmc_slot); | |
379 | ||
380 | int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask), | |
381 | void *data) | |
382 | { | |
383 | int ret = 0; | |
384 | ||
385 | the_menelaus->mmc_callback_data = data; | |
386 | the_menelaus->mmc_callback = callback; | |
387 | ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ, | |
388 | menelaus_mmc_cd_work); | |
389 | if (ret < 0) | |
390 | return ret; | |
391 | ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ, | |
392 | menelaus_mmc_cd_work); | |
393 | if (ret < 0) | |
394 | return ret; | |
395 | ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ, | |
396 | menelaus_mmc_cd_work); | |
397 | if (ret < 0) | |
398 | return ret; | |
399 | ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ, | |
400 | menelaus_mmc_cd_work); | |
401 | ||
402 | return ret; | |
403 | } | |
404 | EXPORT_SYMBOL(menelaus_register_mmc_callback); | |
405 | ||
406 | void menelaus_unregister_mmc_callback(void) | |
407 | { | |
408 | menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ); | |
409 | menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ); | |
410 | menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ); | |
411 | menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ); | |
412 | ||
413 | the_menelaus->mmc_callback = NULL; | |
414 | the_menelaus->mmc_callback_data = 0; | |
415 | } | |
416 | EXPORT_SYMBOL(menelaus_unregister_mmc_callback); | |
417 | ||
418 | struct menelaus_vtg { | |
419 | const char *name; | |
420 | u8 vtg_reg; | |
421 | u8 vtg_shift; | |
422 | u8 vtg_bits; | |
423 | u8 mode_reg; | |
424 | }; | |
425 | ||
426 | struct menelaus_vtg_value { | |
427 | u16 vtg; | |
428 | u16 val; | |
429 | }; | |
430 | ||
431 | static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV, | |
432 | int vtg_val, int mode) | |
433 | { | |
434 | int val, ret; | |
435 | struct i2c_client *c = the_menelaus->client; | |
436 | ||
437 | mutex_lock(&the_menelaus->lock); | |
438 | if (vtg == 0) | |
439 | goto set_voltage; | |
440 | ||
441 | ret = menelaus_read_reg(vtg->vtg_reg); | |
442 | if (ret < 0) | |
443 | goto out; | |
444 | val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift); | |
445 | val |= vtg_val << vtg->vtg_shift; | |
446 | ||
447 | dev_dbg(&c->dev, "Setting voltage '%s'" | |
448 | "to %d mV (reg 0x%02x, val 0x%02x)\n", | |
449 | vtg->name, mV, vtg->vtg_reg, val); | |
450 | ||
451 | ret = menelaus_write_reg(vtg->vtg_reg, val); | |
452 | if (ret < 0) | |
453 | goto out; | |
454 | set_voltage: | |
455 | ret = menelaus_write_reg(vtg->mode_reg, mode); | |
456 | out: | |
457 | mutex_unlock(&the_menelaus->lock); | |
458 | if (ret == 0) { | |
459 | /* Wait for voltage to stabilize */ | |
460 | msleep(1); | |
461 | } | |
462 | return ret; | |
463 | } | |
464 | ||
465 | static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl, | |
466 | int n) | |
467 | { | |
468 | int i; | |
469 | ||
470 | for (i = 0; i < n; i++, tbl++) | |
471 | if (tbl->vtg == vtg) | |
472 | return tbl->val; | |
473 | return -EINVAL; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Vcore can be programmed in two ways: | |
478 | * SW-controlled: Required voltage is programmed into VCORE_CTRL1 | |
479 | * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3 | |
480 | * and VCORE_CTRL4 | |
481 | * | |
482 | * Call correct 'set' function accordingly | |
483 | */ | |
484 | ||
485 | static const struct menelaus_vtg_value vcore_values[] = { | |
486 | { 1000, 0 }, | |
487 | { 1025, 1 }, | |
488 | { 1050, 2 }, | |
489 | { 1075, 3 }, | |
490 | { 1100, 4 }, | |
491 | { 1125, 5 }, | |
492 | { 1150, 6 }, | |
493 | { 1175, 7 }, | |
494 | { 1200, 8 }, | |
495 | { 1225, 9 }, | |
496 | { 1250, 10 }, | |
497 | { 1275, 11 }, | |
498 | { 1300, 12 }, | |
499 | { 1325, 13 }, | |
500 | { 1350, 14 }, | |
501 | { 1375, 15 }, | |
502 | { 1400, 16 }, | |
503 | { 1425, 17 }, | |
504 | { 1450, 18 }, | |
505 | }; | |
506 | ||
507 | int menelaus_set_vcore_sw(unsigned int mV) | |
508 | { | |
509 | int val, ret; | |
510 | struct i2c_client *c = the_menelaus->client; | |
511 | ||
512 | val = menelaus_get_vtg_value(mV, vcore_values, | |
513 | ARRAY_SIZE(vcore_values)); | |
514 | if (val < 0) | |
515 | return -EINVAL; | |
516 | ||
517 | dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val); | |
518 | ||
519 | /* Set SW mode and the voltage in one go. */ | |
520 | mutex_lock(&the_menelaus->lock); | |
521 | ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val); | |
522 | if (ret == 0) | |
523 | the_menelaus->vcore_hw_mode = 0; | |
524 | mutex_unlock(&the_menelaus->lock); | |
525 | msleep(1); | |
526 | ||
527 | return ret; | |
528 | } | |
529 | ||
530 | int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV) | |
531 | { | |
532 | int fval, rval, val, ret; | |
533 | struct i2c_client *c = the_menelaus->client; | |
534 | ||
535 | rval = menelaus_get_vtg_value(roof_mV, vcore_values, | |
536 | ARRAY_SIZE(vcore_values)); | |
537 | if (rval < 0) | |
538 | return -EINVAL; | |
539 | fval = menelaus_get_vtg_value(floor_mV, vcore_values, | |
540 | ARRAY_SIZE(vcore_values)); | |
541 | if (fval < 0) | |
542 | return -EINVAL; | |
543 | ||
544 | dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n", | |
545 | floor_mV, roof_mV); | |
546 | ||
547 | mutex_lock(&the_menelaus->lock); | |
548 | ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval); | |
549 | if (ret < 0) | |
550 | goto out; | |
551 | ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval); | |
552 | if (ret < 0) | |
553 | goto out; | |
554 | if (!the_menelaus->vcore_hw_mode) { | |
555 | val = menelaus_read_reg(MENELAUS_VCORE_CTRL1); | |
556 | /* HW mode, turn OFF byte comparator */ | |
557 | val |= ((1 << 7) | (1 << 5)); | |
558 | ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val); | |
559 | the_menelaus->vcore_hw_mode = 1; | |
560 | } | |
561 | msleep(1); | |
562 | out: | |
563 | mutex_unlock(&the_menelaus->lock); | |
564 | return ret; | |
565 | } | |
566 | ||
567 | static const struct menelaus_vtg vmem_vtg = { | |
568 | .name = "VMEM", | |
569 | .vtg_reg = MENELAUS_LDO_CTRL1, | |
570 | .vtg_shift = 0, | |
571 | .vtg_bits = 2, | |
572 | .mode_reg = MENELAUS_LDO_CTRL3, | |
573 | }; | |
574 | ||
575 | static const struct menelaus_vtg_value vmem_values[] = { | |
576 | { 1500, 0 }, | |
577 | { 1800, 1 }, | |
578 | { 1900, 2 }, | |
579 | { 2500, 3 }, | |
580 | }; | |
581 | ||
582 | int menelaus_set_vmem(unsigned int mV) | |
583 | { | |
584 | int val; | |
585 | ||
586 | if (mV == 0) | |
587 | return menelaus_set_voltage(&vmem_vtg, 0, 0, 0); | |
588 | ||
589 | val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values)); | |
590 | if (val < 0) | |
591 | return -EINVAL; | |
592 | return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02); | |
593 | } | |
594 | EXPORT_SYMBOL(menelaus_set_vmem); | |
595 | ||
596 | static const struct menelaus_vtg vio_vtg = { | |
597 | .name = "VIO", | |
598 | .vtg_reg = MENELAUS_LDO_CTRL1, | |
599 | .vtg_shift = 2, | |
600 | .vtg_bits = 2, | |
601 | .mode_reg = MENELAUS_LDO_CTRL4, | |
602 | }; | |
603 | ||
604 | static const struct menelaus_vtg_value vio_values[] = { | |
605 | { 1500, 0 }, | |
606 | { 1800, 1 }, | |
607 | { 2500, 2 }, | |
608 | { 2800, 3 }, | |
609 | }; | |
610 | ||
611 | int menelaus_set_vio(unsigned int mV) | |
612 | { | |
613 | int val; | |
614 | ||
615 | if (mV == 0) | |
616 | return menelaus_set_voltage(&vio_vtg, 0, 0, 0); | |
617 | ||
618 | val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values)); | |
619 | if (val < 0) | |
620 | return -EINVAL; | |
621 | return menelaus_set_voltage(&vio_vtg, mV, val, 0x02); | |
622 | } | |
623 | EXPORT_SYMBOL(menelaus_set_vio); | |
624 | ||
625 | static const struct menelaus_vtg_value vdcdc_values[] = { | |
626 | { 1500, 0 }, | |
627 | { 1800, 1 }, | |
628 | { 2000, 2 }, | |
629 | { 2200, 3 }, | |
630 | { 2400, 4 }, | |
631 | { 2800, 5 }, | |
632 | { 3000, 6 }, | |
633 | { 3300, 7 }, | |
634 | }; | |
635 | ||
636 | static const struct menelaus_vtg vdcdc2_vtg = { | |
637 | .name = "VDCDC2", | |
638 | .vtg_reg = MENELAUS_DCDC_CTRL1, | |
639 | .vtg_shift = 0, | |
640 | .vtg_bits = 3, | |
641 | .mode_reg = MENELAUS_DCDC_CTRL2, | |
642 | }; | |
643 | ||
644 | static const struct menelaus_vtg vdcdc3_vtg = { | |
645 | .name = "VDCDC3", | |
646 | .vtg_reg = MENELAUS_DCDC_CTRL1, | |
647 | .vtg_shift = 3, | |
648 | .vtg_bits = 3, | |
649 | .mode_reg = MENELAUS_DCDC_CTRL3, | |
650 | }; | |
651 | ||
652 | int menelaus_set_vdcdc(int dcdc, unsigned int mV) | |
653 | { | |
654 | const struct menelaus_vtg *vtg; | |
655 | int val; | |
656 | ||
657 | if (dcdc != 2 && dcdc != 3) | |
658 | return -EINVAL; | |
659 | if (dcdc == 2) | |
660 | vtg = &vdcdc2_vtg; | |
661 | else | |
662 | vtg = &vdcdc3_vtg; | |
663 | ||
664 | if (mV == 0) | |
665 | return menelaus_set_voltage(vtg, 0, 0, 0); | |
666 | ||
667 | val = menelaus_get_vtg_value(mV, vdcdc_values, | |
668 | ARRAY_SIZE(vdcdc_values)); | |
669 | if (val < 0) | |
670 | return -EINVAL; | |
671 | return menelaus_set_voltage(vtg, mV, val, 0x03); | |
672 | } | |
673 | ||
674 | static const struct menelaus_vtg_value vmmc_values[] = { | |
675 | { 1850, 0 }, | |
676 | { 2800, 1 }, | |
677 | { 3000, 2 }, | |
678 | { 3100, 3 }, | |
679 | }; | |
680 | ||
681 | static const struct menelaus_vtg vmmc_vtg = { | |
682 | .name = "VMMC", | |
683 | .vtg_reg = MENELAUS_LDO_CTRL1, | |
684 | .vtg_shift = 6, | |
685 | .vtg_bits = 2, | |
686 | .mode_reg = MENELAUS_LDO_CTRL7, | |
687 | }; | |
688 | ||
689 | int menelaus_set_vmmc(unsigned int mV) | |
690 | { | |
691 | int val; | |
692 | ||
693 | if (mV == 0) | |
694 | return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0); | |
695 | ||
696 | val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values)); | |
697 | if (val < 0) | |
698 | return -EINVAL; | |
699 | return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02); | |
700 | } | |
701 | EXPORT_SYMBOL(menelaus_set_vmmc); | |
702 | ||
703 | ||
704 | static const struct menelaus_vtg_value vaux_values[] = { | |
705 | { 1500, 0 }, | |
706 | { 1800, 1 }, | |
707 | { 2500, 2 }, | |
708 | { 2800, 3 }, | |
709 | }; | |
710 | ||
711 | static const struct menelaus_vtg vaux_vtg = { | |
712 | .name = "VAUX", | |
713 | .vtg_reg = MENELAUS_LDO_CTRL1, | |
714 | .vtg_shift = 4, | |
715 | .vtg_bits = 2, | |
716 | .mode_reg = MENELAUS_LDO_CTRL6, | |
717 | }; | |
718 | ||
719 | int menelaus_set_vaux(unsigned int mV) | |
720 | { | |
721 | int val; | |
722 | ||
723 | if (mV == 0) | |
724 | return menelaus_set_voltage(&vaux_vtg, 0, 0, 0); | |
725 | ||
726 | val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values)); | |
727 | if (val < 0) | |
728 | return -EINVAL; | |
729 | return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02); | |
730 | } | |
731 | EXPORT_SYMBOL(menelaus_set_vaux); | |
732 | ||
733 | int menelaus_get_slot_pin_states(void) | |
734 | { | |
735 | return menelaus_read_reg(MENELAUS_MCT_PIN_ST); | |
736 | } | |
737 | EXPORT_SYMBOL(menelaus_get_slot_pin_states); | |
738 | ||
739 | int menelaus_set_regulator_sleep(int enable, u32 val) | |
740 | { | |
741 | int t, ret; | |
742 | struct i2c_client *c = the_menelaus->client; | |
743 | ||
744 | mutex_lock(&the_menelaus->lock); | |
745 | ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val); | |
746 | if (ret < 0) | |
747 | goto out; | |
748 | ||
749 | dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val); | |
750 | ||
751 | ret = menelaus_read_reg(MENELAUS_GPIO_CTRL); | |
752 | if (ret < 0) | |
753 | goto out; | |
754 | t = ((1 << 6) | 0x04); | |
755 | if (enable) | |
756 | ret |= t; | |
757 | else | |
758 | ret &= ~t; | |
759 | ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret); | |
760 | out: | |
761 | mutex_unlock(&the_menelaus->lock); | |
762 | return ret; | |
763 | } | |
764 | ||
765 | /*-----------------------------------------------------------------------*/ | |
766 | ||
767 | /* Handles Menelaus interrupts. Does not run in interrupt context */ | |
768 | static void menelaus_work(struct work_struct *_menelaus) | |
769 | { | |
770 | struct menelaus_chip *menelaus = | |
771 | container_of(_menelaus, struct menelaus_chip, work); | |
772 | void (*handler)(struct menelaus_chip *menelaus); | |
773 | ||
774 | while (1) { | |
775 | unsigned isr; | |
776 | ||
777 | isr = (menelaus_read_reg(MENELAUS_INT_STATUS2) | |
778 | & ~menelaus->mask2) << 8; | |
779 | isr |= menelaus_read_reg(MENELAUS_INT_STATUS1) | |
780 | & ~menelaus->mask1; | |
781 | if (!isr) | |
782 | break; | |
783 | ||
784 | while (isr) { | |
785 | int irq = fls(isr) - 1; | |
786 | isr &= ~(1 << irq); | |
787 | ||
788 | mutex_lock(&menelaus->lock); | |
789 | menelaus_disable_irq(irq); | |
790 | menelaus_ack_irq(irq); | |
791 | handler = menelaus->handlers[irq]; | |
792 | if (handler) | |
793 | handler(menelaus); | |
794 | menelaus_enable_irq(irq); | |
795 | mutex_unlock(&menelaus->lock); | |
796 | } | |
797 | } | |
798 | enable_irq(menelaus->client->irq); | |
799 | } | |
800 | ||
801 | /* | |
802 | * We cannot use I2C in interrupt context, so we just schedule work. | |
803 | */ | |
804 | static irqreturn_t menelaus_irq(int irq, void *_menelaus) | |
805 | { | |
806 | struct menelaus_chip *menelaus = _menelaus; | |
807 | ||
808 | disable_irq_nosync(irq); | |
809 | (void)schedule_work(&menelaus->work); | |
810 | ||
811 | return IRQ_HANDLED; | |
812 | } | |
813 | ||
814 | /*-----------------------------------------------------------------------*/ | |
815 | ||
816 | /* | |
817 | * The RTC needs to be set once, then it runs on backup battery power. | |
818 | * It supports alarms, including system wake alarms (from some modes); | |
819 | * and 1/second IRQs if requested. | |
820 | */ | |
821 | #ifdef CONFIG_RTC_DRV_TWL92330 | |
822 | ||
823 | #define RTC_CTRL_RTC_EN (1 << 0) | |
824 | #define RTC_CTRL_AL_EN (1 << 1) | |
825 | #define RTC_CTRL_MODE12 (1 << 2) | |
826 | #define RTC_CTRL_EVERY_MASK (3 << 3) | |
827 | #define RTC_CTRL_EVERY_SEC (0 << 3) | |
828 | #define RTC_CTRL_EVERY_MIN (1 << 3) | |
829 | #define RTC_CTRL_EVERY_HR (2 << 3) | |
830 | #define RTC_CTRL_EVERY_DAY (3 << 3) | |
831 | ||
832 | #define RTC_UPDATE_EVERY 0x08 | |
833 | ||
834 | #define RTC_HR_PM (1 << 7) | |
835 | ||
836 | static void menelaus_to_time(char *regs, struct rtc_time *t) | |
837 | { | |
838 | t->tm_sec = BCD2BIN(regs[0]); | |
839 | t->tm_min = BCD2BIN(regs[1]); | |
840 | if (the_menelaus->rtc_control & RTC_CTRL_MODE12) { | |
841 | t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1; | |
842 | if (regs[2] & RTC_HR_PM) | |
843 | t->tm_hour += 12; | |
844 | } else | |
845 | t->tm_hour = BCD2BIN(regs[2] & 0x3f); | |
846 | t->tm_mday = BCD2BIN(regs[3]); | |
847 | t->tm_mon = BCD2BIN(regs[4]) - 1; | |
848 | t->tm_year = BCD2BIN(regs[5]) + 100; | |
849 | } | |
850 | ||
851 | static int time_to_menelaus(struct rtc_time *t, int regnum) | |
852 | { | |
853 | int hour, status; | |
854 | ||
855 | status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec)); | |
856 | if (status < 0) | |
857 | goto fail; | |
858 | ||
859 | status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min)); | |
860 | if (status < 0) | |
861 | goto fail; | |
862 | ||
863 | if (the_menelaus->rtc_control & RTC_CTRL_MODE12) { | |
864 | hour = t->tm_hour + 1; | |
865 | if (hour > 12) | |
866 | hour = RTC_HR_PM | BIN2BCD(hour - 12); | |
867 | else | |
868 | hour = BIN2BCD(hour); | |
869 | } else | |
870 | hour = BIN2BCD(t->tm_hour); | |
871 | status = menelaus_write_reg(regnum++, hour); | |
872 | if (status < 0) | |
873 | goto fail; | |
874 | ||
875 | status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday)); | |
876 | if (status < 0) | |
877 | goto fail; | |
878 | ||
879 | status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1)); | |
880 | if (status < 0) | |
881 | goto fail; | |
882 | ||
883 | status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100)); | |
884 | if (status < 0) | |
885 | goto fail; | |
886 | ||
887 | return 0; | |
888 | fail: | |
889 | dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n", | |
890 | --regnum, status); | |
891 | return status; | |
892 | } | |
893 | ||
894 | static int menelaus_read_time(struct device *dev, struct rtc_time *t) | |
895 | { | |
896 | struct i2c_msg msg[2]; | |
897 | char regs[7]; | |
898 | int status; | |
899 | ||
900 | /* block read date and time registers */ | |
901 | regs[0] = MENELAUS_RTC_SEC; | |
902 | ||
903 | msg[0].addr = MENELAUS_I2C_ADDRESS; | |
904 | msg[0].flags = 0; | |
905 | msg[0].len = 1; | |
906 | msg[0].buf = regs; | |
907 | ||
908 | msg[1].addr = MENELAUS_I2C_ADDRESS; | |
909 | msg[1].flags = I2C_M_RD; | |
910 | msg[1].len = sizeof(regs); | |
911 | msg[1].buf = regs; | |
912 | ||
913 | status = i2c_transfer(the_menelaus->client->adapter, msg, 2); | |
914 | if (status != 2) { | |
915 | dev_err(dev, "%s error %d\n", "read", status); | |
916 | return -EIO; | |
917 | } | |
918 | ||
919 | menelaus_to_time(regs, t); | |
920 | t->tm_wday = BCD2BIN(regs[6]); | |
921 | ||
922 | return 0; | |
923 | } | |
924 | ||
925 | static int menelaus_set_time(struct device *dev, struct rtc_time *t) | |
926 | { | |
927 | int status; | |
928 | ||
929 | /* write date and time registers */ | |
930 | status = time_to_menelaus(t, MENELAUS_RTC_SEC); | |
931 | if (status < 0) | |
932 | return status; | |
933 | status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday)); | |
934 | if (status < 0) { | |
c1147cc6 | 935 | dev_err(&the_menelaus->client->dev, "rtc write reg %02x " |
0c4a59fe TL |
936 | "err %d\n", MENELAUS_RTC_WKDAY, status); |
937 | return status; | |
938 | } | |
939 | ||
940 | /* now commit the write */ | |
941 | status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY); | |
942 | if (status < 0) | |
943 | dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n", | |
944 | status); | |
945 | ||
946 | return 0; | |
947 | } | |
948 | ||
949 | static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w) | |
950 | { | |
951 | struct i2c_msg msg[2]; | |
952 | char regs[6]; | |
953 | int status; | |
954 | ||
955 | /* block read alarm registers */ | |
956 | regs[0] = MENELAUS_RTC_AL_SEC; | |
957 | ||
958 | msg[0].addr = MENELAUS_I2C_ADDRESS; | |
959 | msg[0].flags = 0; | |
960 | msg[0].len = 1; | |
961 | msg[0].buf = regs; | |
962 | ||
963 | msg[1].addr = MENELAUS_I2C_ADDRESS; | |
964 | msg[1].flags = I2C_M_RD; | |
965 | msg[1].len = sizeof(regs); | |
966 | msg[1].buf = regs; | |
967 | ||
968 | status = i2c_transfer(the_menelaus->client->adapter, msg, 2); | |
969 | if (status != 2) { | |
970 | dev_err(dev, "%s error %d\n", "alarm read", status); | |
971 | return -EIO; | |
972 | } | |
973 | ||
974 | menelaus_to_time(regs, &w->time); | |
975 | ||
976 | w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN); | |
977 | ||
978 | /* NOTE we *could* check if actually pending... */ | |
979 | w->pending = 0; | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
984 | static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w) | |
985 | { | |
986 | int status; | |
987 | ||
988 | if (the_menelaus->client->irq <= 0 && w->enabled) | |
989 | return -ENODEV; | |
990 | ||
991 | /* clear previous alarm enable */ | |
992 | if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) { | |
993 | the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; | |
994 | status = menelaus_write_reg(MENELAUS_RTC_CTRL, | |
995 | the_menelaus->rtc_control); | |
996 | if (status < 0) | |
997 | return status; | |
998 | } | |
999 | ||
1000 | /* write alarm registers */ | |
1001 | status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC); | |
1002 | if (status < 0) | |
1003 | return status; | |
1004 | ||
1005 | /* enable alarm if requested */ | |
1006 | if (w->enabled) { | |
1007 | the_menelaus->rtc_control |= RTC_CTRL_AL_EN; | |
1008 | status = menelaus_write_reg(MENELAUS_RTC_CTRL, | |
1009 | the_menelaus->rtc_control); | |
1010 | } | |
1011 | ||
1012 | return status; | |
1013 | } | |
1014 | ||
1015 | #ifdef CONFIG_RTC_INTF_DEV | |
1016 | ||
1017 | static void menelaus_rtc_update_work(struct menelaus_chip *m) | |
1018 | { | |
1019 | /* report 1/sec update */ | |
1020 | local_irq_disable(); | |
1021 | rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF); | |
1022 | local_irq_enable(); | |
1023 | } | |
1024 | ||
1025 | static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg) | |
1026 | { | |
1027 | int status; | |
1028 | ||
1029 | if (the_menelaus->client->irq <= 0) | |
1030 | return -ENOIOCTLCMD; | |
1031 | ||
1032 | switch (cmd) { | |
1033 | /* alarm IRQ */ | |
1034 | case RTC_AIE_ON: | |
1035 | if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) | |
1036 | return 0; | |
1037 | the_menelaus->rtc_control |= RTC_CTRL_AL_EN; | |
1038 | break; | |
1039 | case RTC_AIE_OFF: | |
1040 | if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN)) | |
1041 | return 0; | |
1042 | the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; | |
1043 | break; | |
1044 | /* 1/second "update" IRQ */ | |
1045 | case RTC_UIE_ON: | |
1046 | if (the_menelaus->uie) | |
1047 | return 0; | |
1048 | status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ); | |
1049 | status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ, | |
1050 | menelaus_rtc_update_work); | |
1051 | if (status == 0) | |
1052 | the_menelaus->uie = 1; | |
1053 | return status; | |
1054 | case RTC_UIE_OFF: | |
1055 | if (!the_menelaus->uie) | |
1056 | return 0; | |
1057 | status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ); | |
1058 | if (status == 0) | |
1059 | the_menelaus->uie = 0; | |
1060 | return status; | |
1061 | default: | |
1062 | return -ENOIOCTLCMD; | |
1063 | } | |
1064 | return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control); | |
1065 | } | |
1066 | ||
1067 | #else | |
1068 | #define menelaus_ioctl NULL | |
1069 | #endif | |
1070 | ||
1071 | /* REVISIT no compensation register support ... */ | |
1072 | ||
1073 | static const struct rtc_class_ops menelaus_rtc_ops = { | |
1074 | .ioctl = menelaus_ioctl, | |
1075 | .read_time = menelaus_read_time, | |
1076 | .set_time = menelaus_set_time, | |
1077 | .read_alarm = menelaus_read_alarm, | |
1078 | .set_alarm = menelaus_set_alarm, | |
1079 | }; | |
1080 | ||
1081 | static void menelaus_rtc_alarm_work(struct menelaus_chip *m) | |
1082 | { | |
1083 | /* report alarm */ | |
1084 | local_irq_disable(); | |
1085 | rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF); | |
1086 | local_irq_enable(); | |
1087 | ||
1088 | /* then disable it; alarms are oneshot */ | |
1089 | the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; | |
1090 | menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control); | |
1091 | } | |
1092 | ||
1093 | static inline void menelaus_rtc_init(struct menelaus_chip *m) | |
1094 | { | |
1095 | int alarm = (m->client->irq > 0); | |
1096 | ||
1097 | /* assume 32KDETEN pin is pulled high */ | |
1098 | if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) { | |
1099 | dev_dbg(&m->client->dev, "no 32k oscillator\n"); | |
1100 | return; | |
1101 | } | |
1102 | ||
1103 | /* support RTC alarm; it can issue wakeups */ | |
1104 | if (alarm) { | |
1105 | if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ, | |
1106 | menelaus_rtc_alarm_work) < 0) { | |
1107 | dev_err(&m->client->dev, "can't handle RTC alarm\n"); | |
1108 | return; | |
1109 | } | |
1110 | device_init_wakeup(&m->client->dev, 1); | |
1111 | } | |
1112 | ||
1113 | /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */ | |
1114 | m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL); | |
1115 | if (!(m->rtc_control & RTC_CTRL_RTC_EN) | |
1116 | || (m->rtc_control & RTC_CTRL_AL_EN) | |
1117 | || (m->rtc_control & RTC_CTRL_EVERY_MASK)) { | |
1118 | if (!(m->rtc_control & RTC_CTRL_RTC_EN)) { | |
1119 | dev_warn(&m->client->dev, "rtc clock needs setting\n"); | |
1120 | m->rtc_control |= RTC_CTRL_RTC_EN; | |
1121 | } | |
1122 | m->rtc_control &= ~RTC_CTRL_EVERY_MASK; | |
1123 | m->rtc_control &= ~RTC_CTRL_AL_EN; | |
1124 | menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control); | |
1125 | } | |
1126 | ||
1127 | m->rtc = rtc_device_register(DRIVER_NAME, | |
1128 | &m->client->dev, | |
1129 | &menelaus_rtc_ops, THIS_MODULE); | |
1130 | if (IS_ERR(m->rtc)) { | |
1131 | if (alarm) { | |
1132 | menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ); | |
1133 | device_init_wakeup(&m->client->dev, 0); | |
1134 | } | |
1135 | dev_err(&m->client->dev, "can't register RTC: %d\n", | |
1136 | (int) PTR_ERR(m->rtc)); | |
1137 | the_menelaus->rtc = NULL; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | #else | |
1142 | ||
1143 | static inline void menelaus_rtc_init(struct menelaus_chip *m) | |
1144 | { | |
1145 | /* nothing */ | |
1146 | } | |
1147 | ||
1148 | #endif | |
1149 | ||
1150 | /*-----------------------------------------------------------------------*/ | |
1151 | ||
1152 | static struct i2c_driver menelaus_i2c_driver; | |
1153 | ||
1154 | static int menelaus_probe(struct i2c_client *client) | |
1155 | { | |
1156 | struct menelaus_chip *menelaus; | |
1157 | int rev = 0, val; | |
1158 | int err = 0; | |
1159 | struct menelaus_platform_data *menelaus_pdata = | |
1160 | client->dev.platform_data; | |
1161 | ||
1162 | if (the_menelaus) { | |
1163 | dev_dbg(&client->dev, "only one %s for now\n", | |
1164 | DRIVER_NAME); | |
1165 | return -ENODEV; | |
1166 | } | |
1167 | ||
1168 | menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL); | |
1169 | if (!menelaus) | |
1170 | return -ENOMEM; | |
1171 | ||
1172 | i2c_set_clientdata(client, menelaus); | |
1173 | ||
1174 | the_menelaus = menelaus; | |
1175 | menelaus->client = client; | |
1176 | ||
1177 | /* If a true probe check the device */ | |
1178 | rev = menelaus_read_reg(MENELAUS_REV); | |
1179 | if (rev < 0) { | |
1180 | pr_err("device not found"); | |
1181 | err = -ENODEV; | |
1182 | goto fail1; | |
1183 | } | |
1184 | ||
1185 | /* Ack and disable all Menelaus interrupts */ | |
1186 | menelaus_write_reg(MENELAUS_INT_ACK1, 0xff); | |
1187 | menelaus_write_reg(MENELAUS_INT_ACK2, 0xff); | |
1188 | menelaus_write_reg(MENELAUS_INT_MASK1, 0xff); | |
1189 | menelaus_write_reg(MENELAUS_INT_MASK2, 0xff); | |
1190 | menelaus->mask1 = 0xff; | |
1191 | menelaus->mask2 = 0xff; | |
1192 | ||
1193 | /* Set output buffer strengths */ | |
1194 | menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73); | |
1195 | ||
1196 | if (client->irq > 0) { | |
1197 | err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED, | |
1198 | DRIVER_NAME, menelaus); | |
1199 | if (err) { | |
1200 | dev_dbg(&client->dev, "can't get IRQ %d, err %d", | |
1201 | client->irq, err); | |
1202 | goto fail1; | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | mutex_init(&menelaus->lock); | |
1207 | INIT_WORK(&menelaus->work, menelaus_work); | |
1208 | ||
1209 | pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f); | |
1210 | ||
1211 | val = menelaus_read_reg(MENELAUS_VCORE_CTRL1); | |
1212 | if (val < 0) | |
1213 | goto fail2; | |
1214 | if (val & (1 << 7)) | |
1215 | menelaus->vcore_hw_mode = 1; | |
1216 | else | |
1217 | menelaus->vcore_hw_mode = 0; | |
1218 | ||
1219 | if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) { | |
1220 | err = menelaus_pdata->late_init(&client->dev); | |
1221 | if (err < 0) | |
1222 | goto fail2; | |
1223 | } | |
1224 | ||
1225 | menelaus_rtc_init(menelaus); | |
1226 | ||
1227 | return 0; | |
1228 | fail2: | |
1229 | free_irq(client->irq, menelaus); | |
1230 | flush_scheduled_work(); | |
1231 | fail1: | |
1232 | kfree(menelaus); | |
1233 | return err; | |
1234 | } | |
1235 | ||
1236 | static int __exit menelaus_remove(struct i2c_client *client) | |
1237 | { | |
1238 | struct menelaus_chip *menelaus = i2c_get_clientdata(client); | |
1239 | ||
1240 | free_irq(client->irq, menelaus); | |
1241 | kfree(menelaus); | |
1242 | i2c_set_clientdata(client, NULL); | |
1243 | the_menelaus = NULL; | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | static struct i2c_driver menelaus_i2c_driver = { | |
1248 | .driver = { | |
1249 | .name = DRIVER_NAME, | |
1250 | }, | |
1251 | .probe = menelaus_probe, | |
1252 | .remove = __exit_p(menelaus_remove), | |
1253 | }; | |
1254 | ||
1255 | static int __init menelaus_init(void) | |
1256 | { | |
1257 | int res; | |
1258 | ||
1259 | res = i2c_add_driver(&menelaus_i2c_driver); | |
1260 | if (res < 0) { | |
1261 | pr_err("driver registration failed\n"); | |
1262 | return res; | |
1263 | } | |
1264 | ||
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | static void __exit menelaus_exit(void) | |
1269 | { | |
1270 | i2c_del_driver(&menelaus_i2c_driver); | |
1271 | ||
1272 | /* FIXME: Shutdown menelaus parts that can be shut down */ | |
1273 | } | |
1274 | ||
1275 | MODULE_AUTHOR("Texas Instruments, Inc. (and others)"); | |
1276 | MODULE_DESCRIPTION("I2C interface for Menelaus."); | |
1277 | MODULE_LICENSE("GPL"); | |
1278 | ||
1279 | module_init(menelaus_init); | |
1280 | module_exit(menelaus_exit); |