menelaus.c 30 KB

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