menelaus.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 <linux/mfd/menelaus.h>
  44. #include <asm/mach/irq.h>
  45. #include <asm/gpio.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 = NULL;
  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. ret = menelaus_read_reg(vtg->vtg_reg);
  414. if (ret < 0)
  415. goto out;
  416. val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
  417. val |= vtg_val << vtg->vtg_shift;
  418. dev_dbg(&c->dev, "Setting voltage '%s'"
  419. "to %d mV (reg 0x%02x, val 0x%02x)\n",
  420. vtg->name, mV, vtg->vtg_reg, val);
  421. ret = menelaus_write_reg(vtg->vtg_reg, val);
  422. if (ret < 0)
  423. goto out;
  424. ret = menelaus_write_reg(vtg->mode_reg, mode);
  425. out:
  426. mutex_unlock(&the_menelaus->lock);
  427. if (ret == 0) {
  428. /* Wait for voltage to stabilize */
  429. msleep(1);
  430. }
  431. return ret;
  432. }
  433. static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
  434. int n)
  435. {
  436. int i;
  437. for (i = 0; i < n; i++, tbl++)
  438. if (tbl->vtg == vtg)
  439. return tbl->val;
  440. return -EINVAL;
  441. }
  442. /*
  443. * Vcore can be programmed in two ways:
  444. * SW-controlled: Required voltage is programmed into VCORE_CTRL1
  445. * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
  446. * and VCORE_CTRL4
  447. *
  448. * Call correct 'set' function accordingly
  449. */
  450. static const struct menelaus_vtg_value vcore_values[] = {
  451. { 1000, 0 },
  452. { 1025, 1 },
  453. { 1050, 2 },
  454. { 1075, 3 },
  455. { 1100, 4 },
  456. { 1125, 5 },
  457. { 1150, 6 },
  458. { 1175, 7 },
  459. { 1200, 8 },
  460. { 1225, 9 },
  461. { 1250, 10 },
  462. { 1275, 11 },
  463. { 1300, 12 },
  464. { 1325, 13 },
  465. { 1350, 14 },
  466. { 1375, 15 },
  467. { 1400, 16 },
  468. { 1425, 17 },
  469. { 1450, 18 },
  470. };
  471. int menelaus_set_vcore_sw(unsigned int mV)
  472. {
  473. int val, ret;
  474. struct i2c_client *c = the_menelaus->client;
  475. val = menelaus_get_vtg_value(mV, vcore_values,
  476. ARRAY_SIZE(vcore_values));
  477. if (val < 0)
  478. return -EINVAL;
  479. dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
  480. /* Set SW mode and the voltage in one go. */
  481. mutex_lock(&the_menelaus->lock);
  482. ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
  483. if (ret == 0)
  484. the_menelaus->vcore_hw_mode = 0;
  485. mutex_unlock(&the_menelaus->lock);
  486. msleep(1);
  487. return ret;
  488. }
  489. int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
  490. {
  491. int fval, rval, val, ret;
  492. struct i2c_client *c = the_menelaus->client;
  493. rval = menelaus_get_vtg_value(roof_mV, vcore_values,
  494. ARRAY_SIZE(vcore_values));
  495. if (rval < 0)
  496. return -EINVAL;
  497. fval = menelaus_get_vtg_value(floor_mV, vcore_values,
  498. ARRAY_SIZE(vcore_values));
  499. if (fval < 0)
  500. return -EINVAL;
  501. dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
  502. floor_mV, roof_mV);
  503. mutex_lock(&the_menelaus->lock);
  504. ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
  505. if (ret < 0)
  506. goto out;
  507. ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
  508. if (ret < 0)
  509. goto out;
  510. if (!the_menelaus->vcore_hw_mode) {
  511. val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
  512. /* HW mode, turn OFF byte comparator */
  513. val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
  514. ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
  515. the_menelaus->vcore_hw_mode = 1;
  516. }
  517. msleep(1);
  518. out:
  519. mutex_unlock(&the_menelaus->lock);
  520. return ret;
  521. }
  522. static const struct menelaus_vtg vmem_vtg = {
  523. .name = "VMEM",
  524. .vtg_reg = MENELAUS_LDO_CTRL1,
  525. .vtg_shift = 0,
  526. .vtg_bits = 2,
  527. .mode_reg = MENELAUS_LDO_CTRL3,
  528. };
  529. static const struct menelaus_vtg_value vmem_values[] = {
  530. { 1500, 0 },
  531. { 1800, 1 },
  532. { 1900, 2 },
  533. { 2500, 3 },
  534. };
  535. int menelaus_set_vmem(unsigned int mV)
  536. {
  537. int val;
  538. if (mV == 0)
  539. return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
  540. val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
  541. if (val < 0)
  542. return -EINVAL;
  543. return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
  544. }
  545. EXPORT_SYMBOL(menelaus_set_vmem);
  546. static const struct menelaus_vtg vio_vtg = {
  547. .name = "VIO",
  548. .vtg_reg = MENELAUS_LDO_CTRL1,
  549. .vtg_shift = 2,
  550. .vtg_bits = 2,
  551. .mode_reg = MENELAUS_LDO_CTRL4,
  552. };
  553. static const struct menelaus_vtg_value vio_values[] = {
  554. { 1500, 0 },
  555. { 1800, 1 },
  556. { 2500, 2 },
  557. { 2800, 3 },
  558. };
  559. int menelaus_set_vio(unsigned int mV)
  560. {
  561. int val;
  562. if (mV == 0)
  563. return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
  564. val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
  565. if (val < 0)
  566. return -EINVAL;
  567. return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
  568. }
  569. EXPORT_SYMBOL(menelaus_set_vio);
  570. static const struct menelaus_vtg_value vdcdc_values[] = {
  571. { 1500, 0 },
  572. { 1800, 1 },
  573. { 2000, 2 },
  574. { 2200, 3 },
  575. { 2400, 4 },
  576. { 2800, 5 },
  577. { 3000, 6 },
  578. { 3300, 7 },
  579. };
  580. static const struct menelaus_vtg vdcdc2_vtg = {
  581. .name = "VDCDC2",
  582. .vtg_reg = MENELAUS_DCDC_CTRL1,
  583. .vtg_shift = 0,
  584. .vtg_bits = 3,
  585. .mode_reg = MENELAUS_DCDC_CTRL2,
  586. };
  587. static const struct menelaus_vtg vdcdc3_vtg = {
  588. .name = "VDCDC3",
  589. .vtg_reg = MENELAUS_DCDC_CTRL1,
  590. .vtg_shift = 3,
  591. .vtg_bits = 3,
  592. .mode_reg = MENELAUS_DCDC_CTRL3,
  593. };
  594. int menelaus_set_vdcdc(int dcdc, unsigned int mV)
  595. {
  596. const struct menelaus_vtg *vtg;
  597. int val;
  598. if (dcdc != 2 && dcdc != 3)
  599. return -EINVAL;
  600. if (dcdc == 2)
  601. vtg = &vdcdc2_vtg;
  602. else
  603. vtg = &vdcdc3_vtg;
  604. if (mV == 0)
  605. return menelaus_set_voltage(vtg, 0, 0, 0);
  606. val = menelaus_get_vtg_value(mV, vdcdc_values,
  607. ARRAY_SIZE(vdcdc_values));
  608. if (val < 0)
  609. return -EINVAL;
  610. return menelaus_set_voltage(vtg, mV, val, 0x03);
  611. }
  612. static const struct menelaus_vtg_value vmmc_values[] = {
  613. { 1850, 0 },
  614. { 2800, 1 },
  615. { 3000, 2 },
  616. { 3100, 3 },
  617. };
  618. static const struct menelaus_vtg vmmc_vtg = {
  619. .name = "VMMC",
  620. .vtg_reg = MENELAUS_LDO_CTRL1,
  621. .vtg_shift = 6,
  622. .vtg_bits = 2,
  623. .mode_reg = MENELAUS_LDO_CTRL7,
  624. };
  625. int menelaus_set_vmmc(unsigned int mV)
  626. {
  627. int val;
  628. if (mV == 0)
  629. return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
  630. val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
  631. if (val < 0)
  632. return -EINVAL;
  633. return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
  634. }
  635. EXPORT_SYMBOL(menelaus_set_vmmc);
  636. static const struct menelaus_vtg_value vaux_values[] = {
  637. { 1500, 0 },
  638. { 1800, 1 },
  639. { 2500, 2 },
  640. { 2800, 3 },
  641. };
  642. static const struct menelaus_vtg vaux_vtg = {
  643. .name = "VAUX",
  644. .vtg_reg = MENELAUS_LDO_CTRL1,
  645. .vtg_shift = 4,
  646. .vtg_bits = 2,
  647. .mode_reg = MENELAUS_LDO_CTRL6,
  648. };
  649. int menelaus_set_vaux(unsigned int mV)
  650. {
  651. int val;
  652. if (mV == 0)
  653. return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
  654. val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
  655. if (val < 0)
  656. return -EINVAL;
  657. return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
  658. }
  659. EXPORT_SYMBOL(menelaus_set_vaux);
  660. int menelaus_get_slot_pin_states(void)
  661. {
  662. return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
  663. }
  664. EXPORT_SYMBOL(menelaus_get_slot_pin_states);
  665. int menelaus_set_regulator_sleep(int enable, u32 val)
  666. {
  667. int t, ret;
  668. struct i2c_client *c = the_menelaus->client;
  669. mutex_lock(&the_menelaus->lock);
  670. ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
  671. if (ret < 0)
  672. goto out;
  673. dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
  674. ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
  675. if (ret < 0)
  676. goto out;
  677. t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
  678. if (enable)
  679. ret |= t;
  680. else
  681. ret &= ~t;
  682. ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
  683. out:
  684. mutex_unlock(&the_menelaus->lock);
  685. return ret;
  686. }
  687. /*-----------------------------------------------------------------------*/
  688. /* Handles Menelaus interrupts. Does not run in interrupt context */
  689. static void menelaus_work(struct work_struct *_menelaus)
  690. {
  691. struct menelaus_chip *menelaus =
  692. container_of(_menelaus, struct menelaus_chip, work);
  693. void (*handler)(struct menelaus_chip *menelaus);
  694. while (1) {
  695. unsigned isr;
  696. isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
  697. & ~menelaus->mask2) << 8;
  698. isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
  699. & ~menelaus->mask1;
  700. if (!isr)
  701. break;
  702. while (isr) {
  703. int irq = fls(isr) - 1;
  704. isr &= ~(1 << irq);
  705. mutex_lock(&menelaus->lock);
  706. menelaus_disable_irq(irq);
  707. menelaus_ack_irq(irq);
  708. handler = menelaus->handlers[irq];
  709. if (handler)
  710. handler(menelaus);
  711. menelaus_enable_irq(irq);
  712. mutex_unlock(&menelaus->lock);
  713. }
  714. }
  715. enable_irq(menelaus->client->irq);
  716. }
  717. /*
  718. * We cannot use I2C in interrupt context, so we just schedule work.
  719. */
  720. static irqreturn_t menelaus_irq(int irq, void *_menelaus)
  721. {
  722. struct menelaus_chip *menelaus = _menelaus;
  723. disable_irq_nosync(irq);
  724. (void)schedule_work(&menelaus->work);
  725. return IRQ_HANDLED;
  726. }
  727. /*-----------------------------------------------------------------------*/
  728. /*
  729. * The RTC needs to be set once, then it runs on backup battery power.
  730. * It supports alarms, including system wake alarms (from some modes);
  731. * and 1/second IRQs if requested.
  732. */
  733. #ifdef CONFIG_RTC_DRV_TWL92330
  734. #define RTC_CTRL_RTC_EN (1 << 0)
  735. #define RTC_CTRL_AL_EN (1 << 1)
  736. #define RTC_CTRL_MODE12 (1 << 2)
  737. #define RTC_CTRL_EVERY_MASK (3 << 3)
  738. #define RTC_CTRL_EVERY_SEC (0 << 3)
  739. #define RTC_CTRL_EVERY_MIN (1 << 3)
  740. #define RTC_CTRL_EVERY_HR (2 << 3)
  741. #define RTC_CTRL_EVERY_DAY (3 << 3)
  742. #define RTC_UPDATE_EVERY 0x08
  743. #define RTC_HR_PM (1 << 7)
  744. static void menelaus_to_time(char *regs, struct rtc_time *t)
  745. {
  746. t->tm_sec = bcd2bin(regs[0]);
  747. t->tm_min = bcd2bin(regs[1]);
  748. if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
  749. t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1;
  750. if (regs[2] & RTC_HR_PM)
  751. t->tm_hour += 12;
  752. } else
  753. t->tm_hour = bcd2bin(regs[2] & 0x3f);
  754. t->tm_mday = bcd2bin(regs[3]);
  755. t->tm_mon = bcd2bin(regs[4]) - 1;
  756. t->tm_year = bcd2bin(regs[5]) + 100;
  757. }
  758. static int time_to_menelaus(struct rtc_time *t, int regnum)
  759. {
  760. int hour, status;
  761. status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec));
  762. if (status < 0)
  763. goto fail;
  764. status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min));
  765. if (status < 0)
  766. goto fail;
  767. if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
  768. hour = t->tm_hour + 1;
  769. if (hour > 12)
  770. hour = RTC_HR_PM | bin2bcd(hour - 12);
  771. else
  772. hour = bin2bcd(hour);
  773. } else
  774. hour = bin2bcd(t->tm_hour);
  775. status = menelaus_write_reg(regnum++, hour);
  776. if (status < 0)
  777. goto fail;
  778. status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday));
  779. if (status < 0)
  780. goto fail;
  781. status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1));
  782. if (status < 0)
  783. goto fail;
  784. status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100));
  785. if (status < 0)
  786. goto fail;
  787. return 0;
  788. fail:
  789. dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
  790. --regnum, status);
  791. return status;
  792. }
  793. static int menelaus_read_time(struct device *dev, struct rtc_time *t)
  794. {
  795. struct i2c_msg msg[2];
  796. char regs[7];
  797. int status;
  798. /* block read date and time registers */
  799. regs[0] = MENELAUS_RTC_SEC;
  800. msg[0].addr = MENELAUS_I2C_ADDRESS;
  801. msg[0].flags = 0;
  802. msg[0].len = 1;
  803. msg[0].buf = regs;
  804. msg[1].addr = MENELAUS_I2C_ADDRESS;
  805. msg[1].flags = I2C_M_RD;
  806. msg[1].len = sizeof(regs);
  807. msg[1].buf = regs;
  808. status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
  809. if (status != 2) {
  810. dev_err(dev, "%s error %d\n", "read", status);
  811. return -EIO;
  812. }
  813. menelaus_to_time(regs, t);
  814. t->tm_wday = bcd2bin(regs[6]);
  815. return 0;
  816. }
  817. static int menelaus_set_time(struct device *dev, struct rtc_time *t)
  818. {
  819. int status;
  820. /* write date and time registers */
  821. status = time_to_menelaus(t, MENELAUS_RTC_SEC);
  822. if (status < 0)
  823. return status;
  824. status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday));
  825. if (status < 0) {
  826. dev_err(&the_menelaus->client->dev, "rtc write reg %02x "
  827. "err %d\n", MENELAUS_RTC_WKDAY, status);
  828. return status;
  829. }
  830. /* now commit the write */
  831. status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
  832. if (status < 0)
  833. dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
  834. status);
  835. return 0;
  836. }
  837. static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
  838. {
  839. struct i2c_msg msg[2];
  840. char regs[6];
  841. int status;
  842. /* block read alarm registers */
  843. regs[0] = MENELAUS_RTC_AL_SEC;
  844. msg[0].addr = MENELAUS_I2C_ADDRESS;
  845. msg[0].flags = 0;
  846. msg[0].len = 1;
  847. msg[0].buf = regs;
  848. msg[1].addr = MENELAUS_I2C_ADDRESS;
  849. msg[1].flags = I2C_M_RD;
  850. msg[1].len = sizeof(regs);
  851. msg[1].buf = regs;
  852. status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
  853. if (status != 2) {
  854. dev_err(dev, "%s error %d\n", "alarm read", status);
  855. return -EIO;
  856. }
  857. menelaus_to_time(regs, &w->time);
  858. w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
  859. /* NOTE we *could* check if actually pending... */
  860. w->pending = 0;
  861. return 0;
  862. }
  863. static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
  864. {
  865. int status;
  866. if (the_menelaus->client->irq <= 0 && w->enabled)
  867. return -ENODEV;
  868. /* clear previous alarm enable */
  869. if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
  870. the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
  871. status = menelaus_write_reg(MENELAUS_RTC_CTRL,
  872. the_menelaus->rtc_control);
  873. if (status < 0)
  874. return status;
  875. }
  876. /* write alarm registers */
  877. status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
  878. if (status < 0)
  879. return status;
  880. /* enable alarm if requested */
  881. if (w->enabled) {
  882. the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
  883. status = menelaus_write_reg(MENELAUS_RTC_CTRL,
  884. the_menelaus->rtc_control);
  885. }
  886. return status;
  887. }
  888. #ifdef CONFIG_RTC_INTF_DEV
  889. static void menelaus_rtc_update_work(struct menelaus_chip *m)
  890. {
  891. /* report 1/sec update */
  892. local_irq_disable();
  893. rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
  894. local_irq_enable();
  895. }
  896. static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
  897. {
  898. int status;
  899. if (the_menelaus->client->irq <= 0)
  900. return -ENOIOCTLCMD;
  901. switch (cmd) {
  902. /* alarm IRQ */
  903. case RTC_AIE_ON:
  904. if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
  905. return 0;
  906. the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
  907. break;
  908. case RTC_AIE_OFF:
  909. if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
  910. return 0;
  911. the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
  912. break;
  913. /* 1/second "update" IRQ */
  914. case RTC_UIE_ON:
  915. if (the_menelaus->uie)
  916. return 0;
  917. status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
  918. status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
  919. menelaus_rtc_update_work);
  920. if (status == 0)
  921. the_menelaus->uie = 1;
  922. return status;
  923. case RTC_UIE_OFF:
  924. if (!the_menelaus->uie)
  925. return 0;
  926. status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
  927. if (status == 0)
  928. the_menelaus->uie = 0;
  929. return status;
  930. default:
  931. return -ENOIOCTLCMD;
  932. }
  933. return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
  934. }
  935. #else
  936. #define menelaus_ioctl NULL
  937. #endif
  938. /* REVISIT no compensation register support ... */
  939. static const struct rtc_class_ops menelaus_rtc_ops = {
  940. .ioctl = menelaus_ioctl,
  941. .read_time = menelaus_read_time,
  942. .set_time = menelaus_set_time,
  943. .read_alarm = menelaus_read_alarm,
  944. .set_alarm = menelaus_set_alarm,
  945. };
  946. static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
  947. {
  948. /* report alarm */
  949. local_irq_disable();
  950. rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
  951. local_irq_enable();
  952. /* then disable it; alarms are oneshot */
  953. the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
  954. menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
  955. }
  956. static inline void menelaus_rtc_init(struct menelaus_chip *m)
  957. {
  958. int alarm = (m->client->irq > 0);
  959. /* assume 32KDETEN pin is pulled high */
  960. if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
  961. dev_dbg(&m->client->dev, "no 32k oscillator\n");
  962. return;
  963. }
  964. /* support RTC alarm; it can issue wakeups */
  965. if (alarm) {
  966. if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
  967. menelaus_rtc_alarm_work) < 0) {
  968. dev_err(&m->client->dev, "can't handle RTC alarm\n");
  969. return;
  970. }
  971. device_init_wakeup(&m->client->dev, 1);
  972. }
  973. /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
  974. m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
  975. if (!(m->rtc_control & RTC_CTRL_RTC_EN)
  976. || (m->rtc_control & RTC_CTRL_AL_EN)
  977. || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
  978. if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
  979. dev_warn(&m->client->dev, "rtc clock needs setting\n");
  980. m->rtc_control |= RTC_CTRL_RTC_EN;
  981. }
  982. m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
  983. m->rtc_control &= ~RTC_CTRL_AL_EN;
  984. menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
  985. }
  986. m->rtc = rtc_device_register(DRIVER_NAME,
  987. &m->client->dev,
  988. &menelaus_rtc_ops, THIS_MODULE);
  989. if (IS_ERR(m->rtc)) {
  990. if (alarm) {
  991. menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
  992. device_init_wakeup(&m->client->dev, 0);
  993. }
  994. dev_err(&m->client->dev, "can't register RTC: %d\n",
  995. (int) PTR_ERR(m->rtc));
  996. the_menelaus->rtc = NULL;
  997. }
  998. }
  999. #else
  1000. static inline void menelaus_rtc_init(struct menelaus_chip *m)
  1001. {
  1002. /* nothing */
  1003. }
  1004. #endif
  1005. /*-----------------------------------------------------------------------*/
  1006. static struct i2c_driver menelaus_i2c_driver;
  1007. static int menelaus_probe(struct i2c_client *client,
  1008. const struct i2c_device_id *id)
  1009. {
  1010. struct menelaus_chip *menelaus;
  1011. int rev = 0;
  1012. int err = 0;
  1013. struct menelaus_platform_data *menelaus_pdata =
  1014. dev_get_platdata(&client->dev);
  1015. if (the_menelaus) {
  1016. dev_dbg(&client->dev, "only one %s for now\n",
  1017. DRIVER_NAME);
  1018. return -ENODEV;
  1019. }
  1020. menelaus = devm_kzalloc(&client->dev, sizeof(*menelaus), GFP_KERNEL);
  1021. if (!menelaus)
  1022. return -ENOMEM;
  1023. i2c_set_clientdata(client, menelaus);
  1024. the_menelaus = menelaus;
  1025. menelaus->client = client;
  1026. /* If a true probe check the device */
  1027. rev = menelaus_read_reg(MENELAUS_REV);
  1028. if (rev < 0) {
  1029. pr_err(DRIVER_NAME ": device not found");
  1030. return -ENODEV;
  1031. }
  1032. /* Ack and disable all Menelaus interrupts */
  1033. menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
  1034. menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
  1035. menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
  1036. menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
  1037. menelaus->mask1 = 0xff;
  1038. menelaus->mask2 = 0xff;
  1039. /* Set output buffer strengths */
  1040. menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
  1041. if (client->irq > 0) {
  1042. err = request_irq(client->irq, menelaus_irq, 0,
  1043. DRIVER_NAME, menelaus);
  1044. if (err) {
  1045. dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
  1046. client->irq, err);
  1047. return err;
  1048. }
  1049. }
  1050. mutex_init(&menelaus->lock);
  1051. INIT_WORK(&menelaus->work, menelaus_work);
  1052. pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
  1053. err = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
  1054. if (err < 0)
  1055. goto fail;
  1056. if (err & BIT(7))
  1057. menelaus->vcore_hw_mode = 1;
  1058. else
  1059. menelaus->vcore_hw_mode = 0;
  1060. if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
  1061. err = menelaus_pdata->late_init(&client->dev);
  1062. if (err < 0)
  1063. goto fail;
  1064. }
  1065. menelaus_rtc_init(menelaus);
  1066. return 0;
  1067. fail:
  1068. free_irq(client->irq, menelaus);
  1069. flush_work(&menelaus->work);
  1070. return err;
  1071. }
  1072. static int __exit menelaus_remove(struct i2c_client *client)
  1073. {
  1074. struct menelaus_chip *menelaus = i2c_get_clientdata(client);
  1075. free_irq(client->irq, menelaus);
  1076. flush_work(&menelaus->work);
  1077. the_menelaus = NULL;
  1078. return 0;
  1079. }
  1080. static const struct i2c_device_id menelaus_id[] = {
  1081. { "menelaus", 0 },
  1082. { }
  1083. };
  1084. MODULE_DEVICE_TABLE(i2c, menelaus_id);
  1085. static struct i2c_driver menelaus_i2c_driver = {
  1086. .driver = {
  1087. .name = DRIVER_NAME,
  1088. },
  1089. .probe = menelaus_probe,
  1090. .remove = __exit_p(menelaus_remove),
  1091. .id_table = menelaus_id,
  1092. };
  1093. module_i2c_driver(menelaus_i2c_driver);
  1094. MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
  1095. MODULE_DESCRIPTION("I2C interface for Menelaus.");
  1096. MODULE_LICENSE("GPL");