ab8500-core.c 23 KB

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