ab8500-core.c 27 KB

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