menelaus.c 30 KB

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