ab8500-core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com>
  7. * Author: Mattias Wallin <mattias.wallin@stericsson.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/abx500.h>
  19. #include <linux/mfd/ab8500.h>
  20. #include <linux/regulator/ab8500.h>
  21. /*
  22. * Interrupt register offsets
  23. * Bank : 0x0E
  24. */
  25. #define AB8500_IT_SOURCE1_REG 0x00
  26. #define AB8500_IT_SOURCE2_REG 0x01
  27. #define AB8500_IT_SOURCE3_REG 0x02
  28. #define AB8500_IT_SOURCE4_REG 0x03
  29. #define AB8500_IT_SOURCE5_REG 0x04
  30. #define AB8500_IT_SOURCE6_REG 0x05
  31. #define AB8500_IT_SOURCE7_REG 0x06
  32. #define AB8500_IT_SOURCE8_REG 0x07
  33. #define AB8500_IT_SOURCE19_REG 0x12
  34. #define AB8500_IT_SOURCE20_REG 0x13
  35. #define AB8500_IT_SOURCE21_REG 0x14
  36. #define AB8500_IT_SOURCE22_REG 0x15
  37. #define AB8500_IT_SOURCE23_REG 0x16
  38. #define AB8500_IT_SOURCE24_REG 0x17
  39. /*
  40. * latch registers
  41. */
  42. #define AB8500_IT_LATCH1_REG 0x20
  43. #define AB8500_IT_LATCH2_REG 0x21
  44. #define AB8500_IT_LATCH3_REG 0x22
  45. #define AB8500_IT_LATCH4_REG 0x23
  46. #define AB8500_IT_LATCH5_REG 0x24
  47. #define AB8500_IT_LATCH6_REG 0x25
  48. #define AB8500_IT_LATCH7_REG 0x26
  49. #define AB8500_IT_LATCH8_REG 0x27
  50. #define AB8500_IT_LATCH9_REG 0x28
  51. #define AB8500_IT_LATCH10_REG 0x29
  52. #define AB8500_IT_LATCH12_REG 0x2B
  53. #define AB8500_IT_LATCH19_REG 0x32
  54. #define AB8500_IT_LATCH20_REG 0x33
  55. #define AB8500_IT_LATCH21_REG 0x34
  56. #define AB8500_IT_LATCH22_REG 0x35
  57. #define AB8500_IT_LATCH23_REG 0x36
  58. #define AB8500_IT_LATCH24_REG 0x37
  59. /*
  60. * mask registers
  61. */
  62. #define AB8500_IT_MASK1_REG 0x40
  63. #define AB8500_IT_MASK2_REG 0x41
  64. #define AB8500_IT_MASK3_REG 0x42
  65. #define AB8500_IT_MASK4_REG 0x43
  66. #define AB8500_IT_MASK5_REG 0x44
  67. #define AB8500_IT_MASK6_REG 0x45
  68. #define AB8500_IT_MASK7_REG 0x46
  69. #define AB8500_IT_MASK8_REG 0x47
  70. #define AB8500_IT_MASK9_REG 0x48
  71. #define AB8500_IT_MASK10_REG 0x49
  72. #define AB8500_IT_MASK11_REG 0x4A
  73. #define AB8500_IT_MASK12_REG 0x4B
  74. #define AB8500_IT_MASK13_REG 0x4C
  75. #define AB8500_IT_MASK14_REG 0x4D
  76. #define AB8500_IT_MASK15_REG 0x4E
  77. #define AB8500_IT_MASK16_REG 0x4F
  78. #define AB8500_IT_MASK17_REG 0x50
  79. #define AB8500_IT_MASK18_REG 0x51
  80. #define AB8500_IT_MASK19_REG 0x52
  81. #define AB8500_IT_MASK20_REG 0x53
  82. #define AB8500_IT_MASK21_REG 0x54
  83. #define AB8500_IT_MASK22_REG 0x55
  84. #define AB8500_IT_MASK23_REG 0x56
  85. #define AB8500_IT_MASK24_REG 0x57
  86. #define AB8500_REV_REG 0x80
  87. #define AB8500_SWITCH_OFF_STATUS 0x00
  88. #define AB8500_TURN_ON_STATUS 0x00
  89. /*
  90. * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
  91. * numbers are indexed into this array with (num / 8).
  92. *
  93. * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
  94. * offset 0.
  95. */
  96. static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
  97. 0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 18, 19, 20, 21,
  98. };
  99. static int ab8500_get_chip_id(struct device *dev)
  100. {
  101. struct ab8500 *ab8500;
  102. if (!dev)
  103. return -EINVAL;
  104. ab8500 = dev_get_drvdata(dev->parent);
  105. return ab8500 ? (int)ab8500->chip_id : -EINVAL;
  106. }
  107. static int set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  108. u8 reg, u8 data)
  109. {
  110. int ret;
  111. /*
  112. * Put the u8 bank and u8 register together into a an u16.
  113. * The bank on higher 8 bits and register in lower 8 bits.
  114. * */
  115. u16 addr = ((u16)bank) << 8 | reg;
  116. dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
  117. ret = mutex_lock_interruptible(&ab8500->lock);
  118. if (ret)
  119. return ret;
  120. ret = ab8500->write(ab8500, addr, data);
  121. if (ret < 0)
  122. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  123. addr, ret);
  124. mutex_unlock(&ab8500->lock);
  125. return ret;
  126. }
  127. static int ab8500_set_register(struct device *dev, u8 bank,
  128. u8 reg, u8 value)
  129. {
  130. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  131. return set_register_interruptible(ab8500, bank, reg, value);
  132. }
  133. static int get_register_interruptible(struct ab8500 *ab8500, u8 bank,
  134. u8 reg, u8 *value)
  135. {
  136. int ret;
  137. /* put the u8 bank and u8 reg together into a an u16.
  138. * bank on higher 8 bits and reg in lower */
  139. u16 addr = ((u16)bank) << 8 | reg;
  140. ret = mutex_lock_interruptible(&ab8500->lock);
  141. if (ret)
  142. return ret;
  143. ret = ab8500->read(ab8500, addr);
  144. if (ret < 0)
  145. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  146. addr, ret);
  147. else
  148. *value = ret;
  149. mutex_unlock(&ab8500->lock);
  150. dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
  151. return ret;
  152. }
  153. static int ab8500_get_register(struct device *dev, u8 bank,
  154. u8 reg, u8 *value)
  155. {
  156. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  157. return get_register_interruptible(ab8500, bank, reg, value);
  158. }
  159. static int mask_and_set_register_interruptible(struct ab8500 *ab8500, u8 bank,
  160. u8 reg, u8 bitmask, u8 bitvalues)
  161. {
  162. int ret;
  163. u8 data;
  164. /* put the u8 bank and u8 reg together into a an u16.
  165. * bank on higher 8 bits and reg in lower */
  166. u16 addr = ((u16)bank) << 8 | reg;
  167. ret = mutex_lock_interruptible(&ab8500->lock);
  168. if (ret)
  169. return ret;
  170. ret = ab8500->read(ab8500, addr);
  171. if (ret < 0) {
  172. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  173. addr, ret);
  174. goto out;
  175. }
  176. data = (u8)ret;
  177. data = (~bitmask & data) | (bitmask & bitvalues);
  178. ret = ab8500->write(ab8500, addr, data);
  179. if (ret < 0)
  180. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  181. addr, ret);
  182. dev_vdbg(ab8500->dev, "mask: addr %#x => data %#x\n", addr, data);
  183. out:
  184. mutex_unlock(&ab8500->lock);
  185. return ret;
  186. }
  187. static int ab8500_mask_and_set_register(struct device *dev,
  188. u8 bank, u8 reg, u8 bitmask, u8 bitvalues)
  189. {
  190. struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
  191. return mask_and_set_register_interruptible(ab8500, bank, reg,
  192. bitmask, bitvalues);
  193. }
  194. static struct abx500_ops ab8500_ops = {
  195. .get_chip_id = ab8500_get_chip_id,
  196. .get_register = ab8500_get_register,
  197. .set_register = ab8500_set_register,
  198. .get_register_page = NULL,
  199. .set_register_page = NULL,
  200. .mask_and_set_register = ab8500_mask_and_set_register,
  201. .event_registers_startup_state_get = NULL,
  202. .startup_irq_enabled = NULL,
  203. };
  204. static void ab8500_irq_lock(struct irq_data *data)
  205. {
  206. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  207. mutex_lock(&ab8500->irq_lock);
  208. }
  209. static void ab8500_irq_sync_unlock(struct irq_data *data)
  210. {
  211. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  212. int i;
  213. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
  214. u8 old = ab8500->oldmask[i];
  215. u8 new = ab8500->mask[i];
  216. int reg;
  217. if (new == old)
  218. continue;
  219. /* Interrupt register 12 doesn't exist prior to version 2.0 */
  220. if (ab8500_irq_regoffset[i] == 11 &&
  221. ab8500->chip_id < AB8500_CUT2P0)
  222. continue;
  223. ab8500->oldmask[i] = new;
  224. reg = AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i];
  225. set_register_interruptible(ab8500, AB8500_INTERRUPT, reg, new);
  226. }
  227. mutex_unlock(&ab8500->irq_lock);
  228. }
  229. static void ab8500_irq_mask(struct irq_data *data)
  230. {
  231. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  232. int offset = data->irq - ab8500->irq_base;
  233. int index = offset / 8;
  234. int mask = 1 << (offset % 8);
  235. ab8500->mask[index] |= mask;
  236. }
  237. static void ab8500_irq_unmask(struct irq_data *data)
  238. {
  239. struct ab8500 *ab8500 = irq_data_get_irq_chip_data(data);
  240. int offset = data->irq - ab8500->irq_base;
  241. int index = offset / 8;
  242. int mask = 1 << (offset % 8);
  243. ab8500->mask[index] &= ~mask;
  244. }
  245. static struct irq_chip ab8500_irq_chip = {
  246. .name = "ab8500",
  247. .irq_bus_lock = ab8500_irq_lock,
  248. .irq_bus_sync_unlock = ab8500_irq_sync_unlock,
  249. .irq_mask = ab8500_irq_mask,
  250. .irq_disable = ab8500_irq_mask,
  251. .irq_unmask = ab8500_irq_unmask,
  252. };
  253. static irqreturn_t ab8500_irq(int irq, void *dev)
  254. {
  255. struct ab8500 *ab8500 = dev;
  256. int i;
  257. dev_vdbg(ab8500->dev, "interrupt\n");
  258. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
  259. int regoffset = ab8500_irq_regoffset[i];
  260. int status;
  261. u8 value;
  262. /* Interrupt register 12 doesn't exist prior to version 2.0 */
  263. if (regoffset == 11 && ab8500->chip_id < AB8500_CUT2P0)
  264. continue;
  265. status = get_register_interruptible(ab8500, AB8500_INTERRUPT,
  266. AB8500_IT_LATCH1_REG + regoffset, &value);
  267. if (status < 0 || value == 0)
  268. continue;
  269. do {
  270. int bit = __ffs(value);
  271. int line = i * 8 + bit;
  272. handle_nested_irq(ab8500->irq_base + line);
  273. value &= ~(1 << bit);
  274. } while (value);
  275. }
  276. return IRQ_HANDLED;
  277. }
  278. static int ab8500_irq_init(struct ab8500 *ab8500)
  279. {
  280. int base = ab8500->irq_base;
  281. int irq;
  282. for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
  283. irq_set_chip_data(irq, ab8500);
  284. irq_set_chip_and_handler(irq, &ab8500_irq_chip,
  285. handle_simple_irq);
  286. irq_set_nested_thread(irq, 1);
  287. #ifdef CONFIG_ARM
  288. set_irq_flags(irq, IRQF_VALID);
  289. #else
  290. irq_set_noprobe(irq);
  291. #endif
  292. }
  293. return 0;
  294. }
  295. static void ab8500_irq_remove(struct ab8500 *ab8500)
  296. {
  297. int base = ab8500->irq_base;
  298. int irq;
  299. for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
  300. #ifdef CONFIG_ARM
  301. set_irq_flags(irq, 0);
  302. #endif
  303. irq_set_chip_and_handler(irq, NULL, NULL);
  304. irq_set_chip_data(irq, NULL);
  305. }
  306. }
  307. static struct resource __devinitdata ab8500_gpio_resources[] = {
  308. {
  309. .name = "GPIO_INT6",
  310. .start = AB8500_INT_GPIO6R,
  311. .end = AB8500_INT_GPIO41F,
  312. .flags = IORESOURCE_IRQ,
  313. }
  314. };
  315. static struct resource __devinitdata ab8500_gpadc_resources[] = {
  316. {
  317. .name = "HW_CONV_END",
  318. .start = AB8500_INT_GP_HW_ADC_CONV_END,
  319. .end = AB8500_INT_GP_HW_ADC_CONV_END,
  320. .flags = IORESOURCE_IRQ,
  321. },
  322. {
  323. .name = "SW_CONV_END",
  324. .start = AB8500_INT_GP_SW_ADC_CONV_END,
  325. .end = AB8500_INT_GP_SW_ADC_CONV_END,
  326. .flags = IORESOURCE_IRQ,
  327. },
  328. };
  329. static struct resource __devinitdata ab8500_rtc_resources[] = {
  330. {
  331. .name = "60S",
  332. .start = AB8500_INT_RTC_60S,
  333. .end = AB8500_INT_RTC_60S,
  334. .flags = IORESOURCE_IRQ,
  335. },
  336. {
  337. .name = "ALARM",
  338. .start = AB8500_INT_RTC_ALARM,
  339. .end = AB8500_INT_RTC_ALARM,
  340. .flags = IORESOURCE_IRQ,
  341. },
  342. };
  343. static struct resource __devinitdata ab8500_poweronkey_db_resources[] = {
  344. {
  345. .name = "ONKEY_DBF",
  346. .start = AB8500_INT_PON_KEY1DB_F,
  347. .end = AB8500_INT_PON_KEY1DB_F,
  348. .flags = IORESOURCE_IRQ,
  349. },
  350. {
  351. .name = "ONKEY_DBR",
  352. .start = AB8500_INT_PON_KEY1DB_R,
  353. .end = AB8500_INT_PON_KEY1DB_R,
  354. .flags = IORESOURCE_IRQ,
  355. },
  356. };
  357. static struct resource __devinitdata ab8500_av_acc_detect_resources[] = {
  358. {
  359. .name = "ACC_DETECT_1DB_F",
  360. .start = AB8500_INT_ACC_DETECT_1DB_F,
  361. .end = AB8500_INT_ACC_DETECT_1DB_F,
  362. .flags = IORESOURCE_IRQ,
  363. },
  364. {
  365. .name = "ACC_DETECT_1DB_R",
  366. .start = AB8500_INT_ACC_DETECT_1DB_R,
  367. .end = AB8500_INT_ACC_DETECT_1DB_R,
  368. .flags = IORESOURCE_IRQ,
  369. },
  370. {
  371. .name = "ACC_DETECT_21DB_F",
  372. .start = AB8500_INT_ACC_DETECT_21DB_F,
  373. .end = AB8500_INT_ACC_DETECT_21DB_F,
  374. .flags = IORESOURCE_IRQ,
  375. },
  376. {
  377. .name = "ACC_DETECT_21DB_R",
  378. .start = AB8500_INT_ACC_DETECT_21DB_R,
  379. .end = AB8500_INT_ACC_DETECT_21DB_R,
  380. .flags = IORESOURCE_IRQ,
  381. },
  382. {
  383. .name = "ACC_DETECT_22DB_F",
  384. .start = AB8500_INT_ACC_DETECT_22DB_F,
  385. .end = AB8500_INT_ACC_DETECT_22DB_F,
  386. .flags = IORESOURCE_IRQ,
  387. },
  388. {
  389. .name = "ACC_DETECT_22DB_R",
  390. .start = AB8500_INT_ACC_DETECT_22DB_R,
  391. .end = AB8500_INT_ACC_DETECT_22DB_R,
  392. .flags = IORESOURCE_IRQ,
  393. },
  394. };
  395. static struct resource __devinitdata ab8500_charger_resources[] = {
  396. {
  397. .name = "MAIN_CH_UNPLUG_DET",
  398. .start = AB8500_INT_MAIN_CH_UNPLUG_DET,
  399. .end = AB8500_INT_MAIN_CH_UNPLUG_DET,
  400. .flags = IORESOURCE_IRQ,
  401. },
  402. {
  403. .name = "MAIN_CHARGE_PLUG_DET",
  404. .start = AB8500_INT_MAIN_CH_PLUG_DET,
  405. .end = AB8500_INT_MAIN_CH_PLUG_DET,
  406. .flags = IORESOURCE_IRQ,
  407. },
  408. {
  409. .name = "VBUS_DET_R",
  410. .start = AB8500_INT_VBUS_DET_R,
  411. .end = AB8500_INT_VBUS_DET_R,
  412. .flags = IORESOURCE_IRQ,
  413. },
  414. {
  415. .name = "VBUS_DET_F",
  416. .start = AB8500_INT_VBUS_DET_F,
  417. .end = AB8500_INT_VBUS_DET_F,
  418. .flags = IORESOURCE_IRQ,
  419. },
  420. {
  421. .name = "USB_LINK_STATUS",
  422. .start = AB8500_INT_USB_LINK_STATUS,
  423. .end = AB8500_INT_USB_LINK_STATUS,
  424. .flags = IORESOURCE_IRQ,
  425. },
  426. {
  427. .name = "USB_CHARGE_DET_DONE",
  428. .start = AB8500_INT_USB_CHG_DET_DONE,
  429. .end = AB8500_INT_USB_CHG_DET_DONE,
  430. .flags = IORESOURCE_IRQ,
  431. },
  432. {
  433. .name = "VBUS_OVV",
  434. .start = AB8500_INT_VBUS_OVV,
  435. .end = AB8500_INT_VBUS_OVV,
  436. .flags = IORESOURCE_IRQ,
  437. },
  438. {
  439. .name = "USB_CH_TH_PROT_R",
  440. .start = AB8500_INT_USB_CH_TH_PROT_R,
  441. .end = AB8500_INT_USB_CH_TH_PROT_R,
  442. .flags = IORESOURCE_IRQ,
  443. },
  444. {
  445. .name = "USB_CH_TH_PROT_F",
  446. .start = AB8500_INT_USB_CH_TH_PROT_F,
  447. .end = AB8500_INT_USB_CH_TH_PROT_F,
  448. .flags = IORESOURCE_IRQ,
  449. },
  450. {
  451. .name = "MAIN_EXT_CH_NOT_OK",
  452. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  453. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  454. .flags = IORESOURCE_IRQ,
  455. },
  456. {
  457. .name = "MAIN_CH_TH_PROT_R",
  458. .start = AB8500_INT_MAIN_CH_TH_PROT_R,
  459. .end = AB8500_INT_MAIN_CH_TH_PROT_R,
  460. .flags = IORESOURCE_IRQ,
  461. },
  462. {
  463. .name = "MAIN_CH_TH_PROT_F",
  464. .start = AB8500_INT_MAIN_CH_TH_PROT_F,
  465. .end = AB8500_INT_MAIN_CH_TH_PROT_F,
  466. .flags = IORESOURCE_IRQ,
  467. },
  468. {
  469. .name = "USB_CHARGER_NOT_OKR",
  470. .start = AB8500_INT_USB_CHARGER_NOT_OK,
  471. .end = AB8500_INT_USB_CHARGER_NOT_OK,
  472. .flags = IORESOURCE_IRQ,
  473. },
  474. {
  475. .name = "USB_CHARGER_NOT_OKF",
  476. .start = AB8500_INT_USB_CHARGER_NOT_OKF,
  477. .end = AB8500_INT_USB_CHARGER_NOT_OKF,
  478. .flags = IORESOURCE_IRQ,
  479. },
  480. {
  481. .name = "CH_WD_EXP",
  482. .start = AB8500_INT_CH_WD_EXP,
  483. .end = AB8500_INT_CH_WD_EXP,
  484. .flags = IORESOURCE_IRQ,
  485. },
  486. };
  487. static struct resource __devinitdata ab8500_btemp_resources[] = {
  488. {
  489. .name = "BAT_CTRL_INDB",
  490. .start = AB8500_INT_BAT_CTRL_INDB,
  491. .end = AB8500_INT_BAT_CTRL_INDB,
  492. .flags = IORESOURCE_IRQ,
  493. },
  494. {
  495. .name = "BTEMP_LOW",
  496. .start = AB8500_INT_BTEMP_LOW,
  497. .end = AB8500_INT_BTEMP_LOW,
  498. .flags = IORESOURCE_IRQ,
  499. },
  500. {
  501. .name = "BTEMP_HIGH",
  502. .start = AB8500_INT_BTEMP_HIGH,
  503. .end = AB8500_INT_BTEMP_HIGH,
  504. .flags = IORESOURCE_IRQ,
  505. },
  506. {
  507. .name = "BTEMP_LOW_MEDIUM",
  508. .start = AB8500_INT_BTEMP_LOW_MEDIUM,
  509. .end = AB8500_INT_BTEMP_LOW_MEDIUM,
  510. .flags = IORESOURCE_IRQ,
  511. },
  512. {
  513. .name = "BTEMP_MEDIUM_HIGH",
  514. .start = AB8500_INT_BTEMP_MEDIUM_HIGH,
  515. .end = AB8500_INT_BTEMP_MEDIUM_HIGH,
  516. .flags = IORESOURCE_IRQ,
  517. },
  518. };
  519. static struct resource __devinitdata ab8500_fg_resources[] = {
  520. {
  521. .name = "NCONV_ACCU",
  522. .start = AB8500_INT_CCN_CONV_ACC,
  523. .end = AB8500_INT_CCN_CONV_ACC,
  524. .flags = IORESOURCE_IRQ,
  525. },
  526. {
  527. .name = "BATT_OVV",
  528. .start = AB8500_INT_BATT_OVV,
  529. .end = AB8500_INT_BATT_OVV,
  530. .flags = IORESOURCE_IRQ,
  531. },
  532. {
  533. .name = "LOW_BAT_F",
  534. .start = AB8500_INT_LOW_BAT_F,
  535. .end = AB8500_INT_LOW_BAT_F,
  536. .flags = IORESOURCE_IRQ,
  537. },
  538. {
  539. .name = "LOW_BAT_R",
  540. .start = AB8500_INT_LOW_BAT_R,
  541. .end = AB8500_INT_LOW_BAT_R,
  542. .flags = IORESOURCE_IRQ,
  543. },
  544. {
  545. .name = "CC_INT_CALIB",
  546. .start = AB8500_INT_CC_INT_CALIB,
  547. .end = AB8500_INT_CC_INT_CALIB,
  548. .flags = IORESOURCE_IRQ,
  549. },
  550. };
  551. static struct resource __devinitdata ab8500_chargalg_resources[] = {};
  552. static struct resource __devinitdata ab8500_debug_resources[] = {
  553. {
  554. .name = "IRQ_FIRST",
  555. .start = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  556. .end = AB8500_INT_MAIN_EXT_CH_NOT_OK,
  557. .flags = IORESOURCE_IRQ,
  558. },
  559. {
  560. .name = "IRQ_LAST",
  561. .start = AB8500_INT_USB_CHARGER_NOT_OKF,
  562. .end = AB8500_INT_USB_CHARGER_NOT_OKF,
  563. .flags = IORESOURCE_IRQ,
  564. },
  565. };
  566. static struct resource __devinitdata ab8500_usb_resources[] = {
  567. {
  568. .name = "ID_WAKEUP_R",
  569. .start = AB8500_INT_ID_WAKEUP_R,
  570. .end = AB8500_INT_ID_WAKEUP_R,
  571. .flags = IORESOURCE_IRQ,
  572. },
  573. {
  574. .name = "ID_WAKEUP_F",
  575. .start = AB8500_INT_ID_WAKEUP_F,
  576. .end = AB8500_INT_ID_WAKEUP_F,
  577. .flags = IORESOURCE_IRQ,
  578. },
  579. {
  580. .name = "VBUS_DET_F",
  581. .start = AB8500_INT_VBUS_DET_F,
  582. .end = AB8500_INT_VBUS_DET_F,
  583. .flags = IORESOURCE_IRQ,
  584. },
  585. {
  586. .name = "VBUS_DET_R",
  587. .start = AB8500_INT_VBUS_DET_R,
  588. .end = AB8500_INT_VBUS_DET_R,
  589. .flags = IORESOURCE_IRQ,
  590. },
  591. {
  592. .name = "USB_LINK_STATUS",
  593. .start = AB8500_INT_USB_LINK_STATUS,
  594. .end = AB8500_INT_USB_LINK_STATUS,
  595. .flags = IORESOURCE_IRQ,
  596. },
  597. {
  598. .name = "USB_ADP_PROBE_PLUG",
  599. .start = AB8500_INT_ADP_PROBE_PLUG,
  600. .end = AB8500_INT_ADP_PROBE_PLUG,
  601. .flags = IORESOURCE_IRQ,
  602. },
  603. {
  604. .name = "USB_ADP_PROBE_UNPLUG",
  605. .start = AB8500_INT_ADP_PROBE_UNPLUG,
  606. .end = AB8500_INT_ADP_PROBE_UNPLUG,
  607. .flags = IORESOURCE_IRQ,
  608. },
  609. };
  610. static struct resource __devinitdata ab8500_temp_resources[] = {
  611. {
  612. .name = "AB8500_TEMP_WARM",
  613. .start = AB8500_INT_TEMP_WARM,
  614. .end = AB8500_INT_TEMP_WARM,
  615. .flags = IORESOURCE_IRQ,
  616. },
  617. };
  618. static struct mfd_cell __devinitdata ab8500_devs[] = {
  619. #ifdef CONFIG_DEBUG_FS
  620. {
  621. .name = "ab8500-debug",
  622. .num_resources = ARRAY_SIZE(ab8500_debug_resources),
  623. .resources = ab8500_debug_resources,
  624. },
  625. #endif
  626. {
  627. .name = "ab8500-sysctrl",
  628. },
  629. {
  630. .name = "ab8500-regulator",
  631. },
  632. {
  633. .name = "ab8500-gpio",
  634. .num_resources = ARRAY_SIZE(ab8500_gpio_resources),
  635. .resources = ab8500_gpio_resources,
  636. },
  637. {
  638. .name = "ab8500-gpadc",
  639. .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
  640. .resources = ab8500_gpadc_resources,
  641. },
  642. {
  643. .name = "ab8500-rtc",
  644. .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
  645. .resources = ab8500_rtc_resources,
  646. },
  647. {
  648. .name = "ab8500-charger",
  649. .num_resources = ARRAY_SIZE(ab8500_charger_resources),
  650. .resources = ab8500_charger_resources,
  651. },
  652. {
  653. .name = "ab8500-btemp",
  654. .num_resources = ARRAY_SIZE(ab8500_btemp_resources),
  655. .resources = ab8500_btemp_resources,
  656. },
  657. {
  658. .name = "ab8500-fg",
  659. .num_resources = ARRAY_SIZE(ab8500_fg_resources),
  660. .resources = ab8500_fg_resources,
  661. },
  662. {
  663. .name = "ab8500-chargalg",
  664. .num_resources = ARRAY_SIZE(ab8500_chargalg_resources),
  665. .resources = ab8500_chargalg_resources,
  666. },
  667. {
  668. .name = "ab8500-acc-det",
  669. .num_resources = ARRAY_SIZE(ab8500_av_acc_detect_resources),
  670. .resources = ab8500_av_acc_detect_resources,
  671. },
  672. {
  673. .name = "ab8500-codec",
  674. },
  675. {
  676. .name = "ab8500-usb",
  677. .num_resources = ARRAY_SIZE(ab8500_usb_resources),
  678. .resources = ab8500_usb_resources,
  679. },
  680. {
  681. .name = "ab8500-poweron-key",
  682. .num_resources = ARRAY_SIZE(ab8500_poweronkey_db_resources),
  683. .resources = ab8500_poweronkey_db_resources,
  684. },
  685. {
  686. .name = "ab8500-pwm",
  687. .id = 1,
  688. },
  689. {
  690. .name = "ab8500-pwm",
  691. .id = 2,
  692. },
  693. {
  694. .name = "ab8500-pwm",
  695. .id = 3,
  696. },
  697. { .name = "ab8500-leds", },
  698. {
  699. .name = "ab8500-denc",
  700. },
  701. {
  702. .name = "ab8500-temp",
  703. .num_resources = ARRAY_SIZE(ab8500_temp_resources),
  704. .resources = ab8500_temp_resources,
  705. },
  706. };
  707. static ssize_t show_chip_id(struct device *dev,
  708. struct device_attribute *attr, char *buf)
  709. {
  710. struct ab8500 *ab8500;
  711. ab8500 = dev_get_drvdata(dev);
  712. return sprintf(buf, "%#x\n", ab8500 ? ab8500->chip_id : -EINVAL);
  713. }
  714. /*
  715. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  716. * 0x01 Swoff bit programming
  717. * 0x02 Thermal protection activation
  718. * 0x04 Vbat lower then BattOk falling threshold
  719. * 0x08 Watchdog expired
  720. * 0x10 Non presence of 32kHz clock
  721. * 0x20 Battery level lower than power on reset threshold
  722. * 0x40 Power on key 1 pressed longer than 10 seconds
  723. * 0x80 DB8500 thermal shutdown
  724. */
  725. static ssize_t show_switch_off_status(struct device *dev,
  726. struct device_attribute *attr, char *buf)
  727. {
  728. int ret;
  729. u8 value;
  730. struct ab8500 *ab8500;
  731. ab8500 = dev_get_drvdata(dev);
  732. ret = get_register_interruptible(ab8500, AB8500_RTC,
  733. AB8500_SWITCH_OFF_STATUS, &value);
  734. if (ret < 0)
  735. return ret;
  736. return sprintf(buf, "%#x\n", value);
  737. }
  738. /*
  739. * ab8500 has turned on due to (TURN_ON_STATUS):
  740. * 0x01 PORnVbat
  741. * 0x02 PonKey1dbF
  742. * 0x04 PonKey2dbF
  743. * 0x08 RTCAlarm
  744. * 0x10 MainChDet
  745. * 0x20 VbusDet
  746. * 0x40 UsbIDDetect
  747. * 0x80 Reserved
  748. */
  749. static ssize_t show_turn_on_status(struct device *dev,
  750. struct device_attribute *attr, char *buf)
  751. {
  752. int ret;
  753. u8 value;
  754. struct ab8500 *ab8500;
  755. ab8500 = dev_get_drvdata(dev);
  756. ret = get_register_interruptible(ab8500, AB8500_SYS_CTRL1_BLOCK,
  757. AB8500_TURN_ON_STATUS, &value);
  758. if (ret < 0)
  759. return ret;
  760. return sprintf(buf, "%#x\n", value);
  761. }
  762. static DEVICE_ATTR(chip_id, S_IRUGO, show_chip_id, NULL);
  763. static DEVICE_ATTR(switch_off_status, S_IRUGO, show_switch_off_status, NULL);
  764. static DEVICE_ATTR(turn_on_status, S_IRUGO, show_turn_on_status, NULL);
  765. static struct attribute *ab8500_sysfs_entries[] = {
  766. &dev_attr_chip_id.attr,
  767. &dev_attr_switch_off_status.attr,
  768. &dev_attr_turn_on_status.attr,
  769. NULL,
  770. };
  771. static struct attribute_group ab8500_attr_group = {
  772. .attrs = ab8500_sysfs_entries,
  773. };
  774. int __devinit ab8500_init(struct ab8500 *ab8500)
  775. {
  776. struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
  777. int ret;
  778. int i;
  779. u8 value;
  780. if (plat)
  781. ab8500->irq_base = plat->irq_base;
  782. mutex_init(&ab8500->lock);
  783. mutex_init(&ab8500->irq_lock);
  784. ret = get_register_interruptible(ab8500, AB8500_MISC,
  785. AB8500_REV_REG, &value);
  786. if (ret < 0)
  787. return ret;
  788. switch (value) {
  789. case AB8500_CUT1P0:
  790. case AB8500_CUT1P1:
  791. case AB8500_CUT2P0:
  792. case AB8500_CUT3P0:
  793. case AB8500_CUT3P3:
  794. dev_info(ab8500->dev, "detected chip, revision: %#x\n", value);
  795. break;
  796. default:
  797. dev_err(ab8500->dev, "unknown chip, revision: %#x\n", value);
  798. return -EINVAL;
  799. }
  800. ab8500->chip_id = value;
  801. /*
  802. * ab8500 has switched off due to (SWITCH_OFF_STATUS):
  803. * 0x01 Swoff bit programming
  804. * 0x02 Thermal protection activation
  805. * 0x04 Vbat lower then BattOk falling threshold
  806. * 0x08 Watchdog expired
  807. * 0x10 Non presence of 32kHz clock
  808. * 0x20 Battery level lower than power on reset threshold
  809. * 0x40 Power on key 1 pressed longer than 10 seconds
  810. * 0x80 DB8500 thermal shutdown
  811. */
  812. ret = get_register_interruptible(ab8500, AB8500_RTC,
  813. AB8500_SWITCH_OFF_STATUS, &value);
  814. if (ret < 0)
  815. return ret;
  816. dev_info(ab8500->dev, "switch off status: %#x", value);
  817. if (plat && plat->init)
  818. plat->init(ab8500);
  819. /* Clear and mask all interrupts */
  820. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
  821. /* Interrupt register 12 doesn't exist prior to version 2.0 */
  822. if (ab8500_irq_regoffset[i] == 11 &&
  823. ab8500->chip_id < AB8500_CUT2P0)
  824. continue;
  825. get_register_interruptible(ab8500, AB8500_INTERRUPT,
  826. AB8500_IT_LATCH1_REG + ab8500_irq_regoffset[i],
  827. &value);
  828. set_register_interruptible(ab8500, AB8500_INTERRUPT,
  829. AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i], 0xff);
  830. }
  831. ret = abx500_register_ops(ab8500->dev, &ab8500_ops);
  832. if (ret)
  833. return ret;
  834. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++)
  835. ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
  836. if (ab8500->irq_base) {
  837. ret = ab8500_irq_init(ab8500);
  838. if (ret)
  839. return ret;
  840. ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
  841. IRQF_ONESHOT | IRQF_NO_SUSPEND,
  842. "ab8500", ab8500);
  843. if (ret)
  844. goto out_removeirq;
  845. }
  846. ret = mfd_add_devices(ab8500->dev, 0, ab8500_devs,
  847. ARRAY_SIZE(ab8500_devs), NULL,
  848. ab8500->irq_base);
  849. if (ret)
  850. goto out_freeirq;
  851. ret = sysfs_create_group(&ab8500->dev->kobj, &ab8500_attr_group);
  852. if (ret)
  853. dev_err(ab8500->dev, "error creating sysfs entries\n");
  854. return ret;
  855. out_freeirq:
  856. if (ab8500->irq_base) {
  857. free_irq(ab8500->irq, ab8500);
  858. out_removeirq:
  859. ab8500_irq_remove(ab8500);
  860. }
  861. return ret;
  862. }
  863. int __devexit ab8500_exit(struct ab8500 *ab8500)
  864. {
  865. sysfs_remove_group(&ab8500->dev->kobj, &ab8500_attr_group);
  866. mfd_remove_devices(ab8500->dev);
  867. if (ab8500->irq_base) {
  868. free_irq(ab8500->irq, ab8500);
  869. ab8500_irq_remove(ab8500);
  870. }
  871. return 0;
  872. }
  873. MODULE_AUTHOR("Mattias Wallin, Srinidhi Kasagar, Rabin Vincent");
  874. MODULE_DESCRIPTION("AB8500 MFD core");
  875. MODULE_LICENSE("GPL v2");