menelaus.c 31 KB

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