menelaus.c 30 KB

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