menelaus.c 29 KB

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