menelaus.c 30 KB

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