ab3100-core.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * Copyright (C) 2007-2010 ST-Ericsson
  3. * License terms: GNU General Public License (GPL) version 2
  4. * Low-level core for exclusive access to the AB3100 IC on the I2C bus
  5. * and some basic chip-configuration.
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/notifier.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/random.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/mfd/ab3100.h>
  24. #include <linux/mfd/abx500.h>
  25. /* These are the only registers inside AB3100 used in this main file */
  26. /* Interrupt event registers */
  27. #define AB3100_EVENTA1 0x21
  28. #define AB3100_EVENTA2 0x22
  29. #define AB3100_EVENTA3 0x23
  30. /* AB3100 DAC converter registers */
  31. #define AB3100_DIS 0x00
  32. #define AB3100_D0C 0x01
  33. #define AB3100_D1C 0x02
  34. #define AB3100_D2C 0x03
  35. #define AB3100_D3C 0x04
  36. /* Chip ID register */
  37. #define AB3100_CID 0x20
  38. /* AB3100 interrupt registers */
  39. #define AB3100_IMRA1 0x24
  40. #define AB3100_IMRA2 0x25
  41. #define AB3100_IMRA3 0x26
  42. #define AB3100_IMRB1 0x2B
  43. #define AB3100_IMRB2 0x2C
  44. #define AB3100_IMRB3 0x2D
  45. /* System Power Monitoring and control registers */
  46. #define AB3100_MCA 0x2E
  47. #define AB3100_MCB 0x2F
  48. /* SIM power up */
  49. #define AB3100_SUP 0x50
  50. /*
  51. * I2C communication
  52. *
  53. * The AB3100 is usually assigned address 0x48 (7-bit)
  54. * The chip is defined in the platform i2c_board_data section.
  55. */
  56. static int ab3100_get_chip_id(struct device *dev)
  57. {
  58. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  59. return (int)ab3100->chip_id;
  60. }
  61. static int ab3100_set_register_interruptible(struct ab3100 *ab3100,
  62. u8 reg, u8 regval)
  63. {
  64. u8 regandval[2] = {reg, regval};
  65. int err;
  66. err = mutex_lock_interruptible(&ab3100->access_mutex);
  67. if (err)
  68. return err;
  69. /*
  70. * A two-byte write message with the first byte containing the register
  71. * number and the second byte containing the value to be written
  72. * effectively sets a register in the AB3100.
  73. */
  74. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  75. if (err < 0) {
  76. dev_err(ab3100->dev,
  77. "write error (write register): %d\n",
  78. err);
  79. } else if (err != 2) {
  80. dev_err(ab3100->dev,
  81. "write error (write register) "
  82. "%d bytes transferred (expected 2)\n",
  83. err);
  84. err = -EIO;
  85. } else {
  86. /* All is well */
  87. err = 0;
  88. }
  89. mutex_unlock(&ab3100->access_mutex);
  90. return err;
  91. }
  92. static int set_register_interruptible(struct device *dev,
  93. u8 bank, u8 reg, u8 value)
  94. {
  95. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  96. return ab3100_set_register_interruptible(ab3100, reg, value);
  97. }
  98. /*
  99. * The test registers exist at an I2C bus address up one
  100. * from the ordinary base. They are not supposed to be used
  101. * in production code, but sometimes you have to do that
  102. * anyway. It's currently only used from this file so declare
  103. * it static and do not export.
  104. */
  105. static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
  106. u8 reg, u8 regval)
  107. {
  108. u8 regandval[2] = {reg, regval};
  109. int err;
  110. err = mutex_lock_interruptible(&ab3100->access_mutex);
  111. if (err)
  112. return err;
  113. err = i2c_master_send(ab3100->testreg_client, regandval, 2);
  114. if (err < 0) {
  115. dev_err(ab3100->dev,
  116. "write error (write test register): %d\n",
  117. err);
  118. } else if (err != 2) {
  119. dev_err(ab3100->dev,
  120. "write error (write test register) "
  121. "%d bytes transferred (expected 2)\n",
  122. err);
  123. err = -EIO;
  124. } else {
  125. /* All is well */
  126. err = 0;
  127. }
  128. mutex_unlock(&ab3100->access_mutex);
  129. return err;
  130. }
  131. static int ab3100_get_register_interruptible(struct ab3100 *ab3100,
  132. u8 reg, u8 *regval)
  133. {
  134. int err;
  135. err = mutex_lock_interruptible(&ab3100->access_mutex);
  136. if (err)
  137. return err;
  138. /*
  139. * AB3100 require an I2C "stop" command between each message, else
  140. * it will not work. The only way of achieveing this with the
  141. * message transport layer is to send the read and write messages
  142. * separately.
  143. */
  144. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  145. if (err < 0) {
  146. dev_err(ab3100->dev,
  147. "write error (send register address): %d\n",
  148. err);
  149. goto get_reg_out_unlock;
  150. } else if (err != 1) {
  151. dev_err(ab3100->dev,
  152. "write error (send register address) "
  153. "%d bytes transferred (expected 1)\n",
  154. err);
  155. err = -EIO;
  156. goto get_reg_out_unlock;
  157. } else {
  158. /* All is well */
  159. err = 0;
  160. }
  161. err = i2c_master_recv(ab3100->i2c_client, regval, 1);
  162. if (err < 0) {
  163. dev_err(ab3100->dev,
  164. "write error (read register): %d\n",
  165. err);
  166. goto get_reg_out_unlock;
  167. } else if (err != 1) {
  168. dev_err(ab3100->dev,
  169. "write error (read register) "
  170. "%d bytes transferred (expected 1)\n",
  171. err);
  172. err = -EIO;
  173. goto get_reg_out_unlock;
  174. } else {
  175. /* All is well */
  176. err = 0;
  177. }
  178. get_reg_out_unlock:
  179. mutex_unlock(&ab3100->access_mutex);
  180. return err;
  181. }
  182. static int get_register_interruptible(struct device *dev, u8 bank, u8 reg,
  183. u8 *value)
  184. {
  185. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  186. return ab3100_get_register_interruptible(ab3100, reg, value);
  187. }
  188. static int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
  189. u8 first_reg, u8 *regvals, u8 numregs)
  190. {
  191. int err;
  192. if (ab3100->chip_id == 0xa0 ||
  193. ab3100->chip_id == 0xa1)
  194. /* These don't support paged reads */
  195. return -EIO;
  196. err = mutex_lock_interruptible(&ab3100->access_mutex);
  197. if (err)
  198. return err;
  199. /*
  200. * Paged read also require an I2C "stop" command.
  201. */
  202. err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
  203. if (err < 0) {
  204. dev_err(ab3100->dev,
  205. "write error (send first register address): %d\n",
  206. err);
  207. goto get_reg_page_out_unlock;
  208. } else if (err != 1) {
  209. dev_err(ab3100->dev,
  210. "write error (send first register address) "
  211. "%d bytes transferred (expected 1)\n",
  212. err);
  213. err = -EIO;
  214. goto get_reg_page_out_unlock;
  215. }
  216. err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
  217. if (err < 0) {
  218. dev_err(ab3100->dev,
  219. "write error (read register page): %d\n",
  220. err);
  221. goto get_reg_page_out_unlock;
  222. } else if (err != numregs) {
  223. dev_err(ab3100->dev,
  224. "write error (read register page) "
  225. "%d bytes transferred (expected %d)\n",
  226. err, numregs);
  227. err = -EIO;
  228. goto get_reg_page_out_unlock;
  229. }
  230. /* All is well */
  231. err = 0;
  232. get_reg_page_out_unlock:
  233. mutex_unlock(&ab3100->access_mutex);
  234. return err;
  235. }
  236. static int get_register_page_interruptible(struct device *dev, u8 bank,
  237. u8 first_reg, u8 *regvals, u8 numregs)
  238. {
  239. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  240. return ab3100_get_register_page_interruptible(ab3100,
  241. first_reg, regvals, numregs);
  242. }
  243. static int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
  244. u8 reg, u8 andmask, u8 ormask)
  245. {
  246. u8 regandval[2] = {reg, 0};
  247. int err;
  248. err = mutex_lock_interruptible(&ab3100->access_mutex);
  249. if (err)
  250. return err;
  251. /* First read out the target register */
  252. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  253. if (err < 0) {
  254. dev_err(ab3100->dev,
  255. "write error (maskset send address): %d\n",
  256. err);
  257. goto get_maskset_unlock;
  258. } else if (err != 1) {
  259. dev_err(ab3100->dev,
  260. "write error (maskset send address) "
  261. "%d bytes transferred (expected 1)\n",
  262. err);
  263. err = -EIO;
  264. goto get_maskset_unlock;
  265. }
  266. err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
  267. if (err < 0) {
  268. dev_err(ab3100->dev,
  269. "write error (maskset read register): %d\n",
  270. err);
  271. goto get_maskset_unlock;
  272. } else if (err != 1) {
  273. dev_err(ab3100->dev,
  274. "write error (maskset read register) "
  275. "%d bytes transferred (expected 1)\n",
  276. err);
  277. err = -EIO;
  278. goto get_maskset_unlock;
  279. }
  280. /* Modify the register */
  281. regandval[1] &= andmask;
  282. regandval[1] |= ormask;
  283. /* Write the register */
  284. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  285. if (err < 0) {
  286. dev_err(ab3100->dev,
  287. "write error (write register): %d\n",
  288. err);
  289. goto get_maskset_unlock;
  290. } else if (err != 2) {
  291. dev_err(ab3100->dev,
  292. "write error (write register) "
  293. "%d bytes transferred (expected 2)\n",
  294. err);
  295. err = -EIO;
  296. goto get_maskset_unlock;
  297. }
  298. /* All is well */
  299. err = 0;
  300. get_maskset_unlock:
  301. mutex_unlock(&ab3100->access_mutex);
  302. return err;
  303. }
  304. static int mask_and_set_register_interruptible(struct device *dev, u8 bank,
  305. u8 reg, u8 bitmask, u8 bitvalues)
  306. {
  307. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  308. return ab3100_mask_and_set_register_interruptible(ab3100,
  309. reg, bitmask, (bitmask & bitvalues));
  310. }
  311. /*
  312. * Register a simple callback for handling any AB3100 events.
  313. */
  314. int ab3100_event_register(struct ab3100 *ab3100,
  315. struct notifier_block *nb)
  316. {
  317. return blocking_notifier_chain_register(&ab3100->event_subscribers,
  318. nb);
  319. }
  320. EXPORT_SYMBOL(ab3100_event_register);
  321. /*
  322. * Remove a previously registered callback.
  323. */
  324. int ab3100_event_unregister(struct ab3100 *ab3100,
  325. struct notifier_block *nb)
  326. {
  327. return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
  328. nb);
  329. }
  330. EXPORT_SYMBOL(ab3100_event_unregister);
  331. static int ab3100_event_registers_startup_state_get(struct device *dev,
  332. u8 *event)
  333. {
  334. struct ab3100 *ab3100 = dev_get_drvdata(dev->parent);
  335. if (!ab3100->startup_events_read)
  336. return -EAGAIN; /* Try again later */
  337. memcpy(event, ab3100->startup_events, 3);
  338. return 0;
  339. }
  340. static struct abx500_ops ab3100_ops = {
  341. .get_chip_id = ab3100_get_chip_id,
  342. .set_register = set_register_interruptible,
  343. .get_register = get_register_interruptible,
  344. .get_register_page = get_register_page_interruptible,
  345. .set_register_page = NULL,
  346. .mask_and_set_register = mask_and_set_register_interruptible,
  347. .event_registers_startup_state_get =
  348. ab3100_event_registers_startup_state_get,
  349. .startup_irq_enabled = NULL,
  350. };
  351. /*
  352. * This is a threaded interrupt handler so we can make some
  353. * I2C calls etc.
  354. */
  355. static irqreturn_t ab3100_irq_handler(int irq, void *data)
  356. {
  357. struct ab3100 *ab3100 = data;
  358. u8 event_regs[3];
  359. u32 fatevent;
  360. int err;
  361. err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
  362. event_regs, 3);
  363. if (err)
  364. goto err_event;
  365. fatevent = (event_regs[0] << 16) |
  366. (event_regs[1] << 8) |
  367. event_regs[2];
  368. if (!ab3100->startup_events_read) {
  369. ab3100->startup_events[0] = event_regs[0];
  370. ab3100->startup_events[1] = event_regs[1];
  371. ab3100->startup_events[2] = event_regs[2];
  372. ab3100->startup_events_read = true;
  373. }
  374. /*
  375. * The notified parties will have to mask out the events
  376. * they're interested in and react to them. They will be
  377. * notified on all events, then they use the fatevent value
  378. * to determine if they're interested.
  379. */
  380. blocking_notifier_call_chain(&ab3100->event_subscribers,
  381. fatevent, NULL);
  382. dev_dbg(ab3100->dev,
  383. "IRQ Event: 0x%08x\n", fatevent);
  384. return IRQ_HANDLED;
  385. err_event:
  386. dev_dbg(ab3100->dev,
  387. "error reading event status\n");
  388. return IRQ_HANDLED;
  389. }
  390. #ifdef CONFIG_DEBUG_FS
  391. /*
  392. * Some debugfs entries only exposed if we're using debug
  393. */
  394. static int ab3100_registers_print(struct seq_file *s, void *p)
  395. {
  396. struct ab3100 *ab3100 = s->private;
  397. u8 value;
  398. u8 reg;
  399. seq_printf(s, "AB3100 registers:\n");
  400. for (reg = 0; reg < 0xff; reg++) {
  401. ab3100_get_register_interruptible(ab3100, reg, &value);
  402. seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
  403. }
  404. return 0;
  405. }
  406. static int ab3100_registers_open(struct inode *inode, struct file *file)
  407. {
  408. return single_open(file, ab3100_registers_print, inode->i_private);
  409. }
  410. static const struct file_operations ab3100_registers_fops = {
  411. .open = ab3100_registers_open,
  412. .read = seq_read,
  413. .llseek = seq_lseek,
  414. .release = single_release,
  415. .owner = THIS_MODULE,
  416. };
  417. struct ab3100_get_set_reg_priv {
  418. struct ab3100 *ab3100;
  419. bool mode;
  420. };
  421. static ssize_t ab3100_get_set_reg(struct file *file,
  422. const char __user *user_buf,
  423. size_t count, loff_t *ppos)
  424. {
  425. struct ab3100_get_set_reg_priv *priv = file->private_data;
  426. struct ab3100 *ab3100 = priv->ab3100;
  427. char buf[32];
  428. ssize_t buf_size;
  429. int regp;
  430. unsigned long user_reg;
  431. int err;
  432. int i = 0;
  433. /* Get userspace string and assure termination */
  434. buf_size = min(count, (sizeof(buf)-1));
  435. if (copy_from_user(buf, user_buf, buf_size))
  436. return -EFAULT;
  437. buf[buf_size] = 0;
  438. /*
  439. * The idea is here to parse a string which is either
  440. * "0xnn" for reading a register, or "0xaa 0xbb" for
  441. * writing 0xbb to the register 0xaa. First move past
  442. * whitespace and then begin to parse the register.
  443. */
  444. while ((i < buf_size) && (buf[i] == ' '))
  445. i++;
  446. regp = i;
  447. /*
  448. * Advance pointer to end of string then terminate
  449. * the register string. This is needed to satisfy
  450. * the strict_strtoul() function.
  451. */
  452. while ((i < buf_size) && (buf[i] != ' '))
  453. i++;
  454. buf[i] = '\0';
  455. err = strict_strtoul(&buf[regp], 16, &user_reg);
  456. if (err)
  457. return err;
  458. if (user_reg > 0xff)
  459. return -EINVAL;
  460. /* Either we read or we write a register here */
  461. if (!priv->mode) {
  462. /* Reading */
  463. u8 reg = (u8) user_reg;
  464. u8 regvalue;
  465. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  466. dev_info(ab3100->dev,
  467. "debug read AB3100 reg[0x%02x]: 0x%02x\n",
  468. reg, regvalue);
  469. } else {
  470. int valp;
  471. unsigned long user_value;
  472. u8 reg = (u8) user_reg;
  473. u8 value;
  474. u8 regvalue;
  475. /*
  476. * Writing, we need some value to write to
  477. * the register so keep parsing the string
  478. * from userspace.
  479. */
  480. i++;
  481. while ((i < buf_size) && (buf[i] == ' '))
  482. i++;
  483. valp = i;
  484. while ((i < buf_size) && (buf[i] != ' '))
  485. i++;
  486. buf[i] = '\0';
  487. err = strict_strtoul(&buf[valp], 16, &user_value);
  488. if (err)
  489. return err;
  490. if (user_reg > 0xff)
  491. return -EINVAL;
  492. value = (u8) user_value;
  493. ab3100_set_register_interruptible(ab3100, reg, value);
  494. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  495. dev_info(ab3100->dev,
  496. "debug write reg[0x%02x] with 0x%02x, "
  497. "after readback: 0x%02x\n",
  498. reg, value, regvalue);
  499. }
  500. return buf_size;
  501. }
  502. static const struct file_operations ab3100_get_set_reg_fops = {
  503. .open = simple_open,
  504. .write = ab3100_get_set_reg,
  505. .llseek = noop_llseek,
  506. };
  507. static struct dentry *ab3100_dir;
  508. static struct dentry *ab3100_reg_file;
  509. static struct ab3100_get_set_reg_priv ab3100_get_priv;
  510. static struct dentry *ab3100_get_reg_file;
  511. static struct ab3100_get_set_reg_priv ab3100_set_priv;
  512. static struct dentry *ab3100_set_reg_file;
  513. static void ab3100_setup_debugfs(struct ab3100 *ab3100)
  514. {
  515. int err;
  516. ab3100_dir = debugfs_create_dir("ab3100", NULL);
  517. if (!ab3100_dir)
  518. goto exit_no_debugfs;
  519. ab3100_reg_file = debugfs_create_file("registers",
  520. S_IRUGO, ab3100_dir, ab3100,
  521. &ab3100_registers_fops);
  522. if (!ab3100_reg_file) {
  523. err = -ENOMEM;
  524. goto exit_destroy_dir;
  525. }
  526. ab3100_get_priv.ab3100 = ab3100;
  527. ab3100_get_priv.mode = false;
  528. ab3100_get_reg_file = debugfs_create_file("get_reg",
  529. S_IWUSR, ab3100_dir, &ab3100_get_priv,
  530. &ab3100_get_set_reg_fops);
  531. if (!ab3100_get_reg_file) {
  532. err = -ENOMEM;
  533. goto exit_destroy_reg;
  534. }
  535. ab3100_set_priv.ab3100 = ab3100;
  536. ab3100_set_priv.mode = true;
  537. ab3100_set_reg_file = debugfs_create_file("set_reg",
  538. S_IWUSR, ab3100_dir, &ab3100_set_priv,
  539. &ab3100_get_set_reg_fops);
  540. if (!ab3100_set_reg_file) {
  541. err = -ENOMEM;
  542. goto exit_destroy_get_reg;
  543. }
  544. return;
  545. exit_destroy_get_reg:
  546. debugfs_remove(ab3100_get_reg_file);
  547. exit_destroy_reg:
  548. debugfs_remove(ab3100_reg_file);
  549. exit_destroy_dir:
  550. debugfs_remove(ab3100_dir);
  551. exit_no_debugfs:
  552. return;
  553. }
  554. static inline void ab3100_remove_debugfs(void)
  555. {
  556. debugfs_remove(ab3100_set_reg_file);
  557. debugfs_remove(ab3100_get_reg_file);
  558. debugfs_remove(ab3100_reg_file);
  559. debugfs_remove(ab3100_dir);
  560. }
  561. #else
  562. static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
  563. {
  564. }
  565. static inline void ab3100_remove_debugfs(void)
  566. {
  567. }
  568. #endif
  569. /*
  570. * Basic set-up, datastructure creation/destruction and I2C interface.
  571. * This sets up a default config in the AB3100 chip so that it
  572. * will work as expected.
  573. */
  574. struct ab3100_init_setting {
  575. u8 abreg;
  576. u8 setting;
  577. };
  578. static const struct ab3100_init_setting __devinitconst
  579. ab3100_init_settings[] = {
  580. {
  581. .abreg = AB3100_MCA,
  582. .setting = 0x01
  583. }, {
  584. .abreg = AB3100_MCB,
  585. .setting = 0x30
  586. }, {
  587. .abreg = AB3100_IMRA1,
  588. .setting = 0x00
  589. }, {
  590. .abreg = AB3100_IMRA2,
  591. .setting = 0xFF
  592. }, {
  593. .abreg = AB3100_IMRA3,
  594. .setting = 0x01
  595. }, {
  596. .abreg = AB3100_IMRB1,
  597. .setting = 0xBF
  598. }, {
  599. .abreg = AB3100_IMRB2,
  600. .setting = 0xFF
  601. }, {
  602. .abreg = AB3100_IMRB3,
  603. .setting = 0xFF
  604. }, {
  605. .abreg = AB3100_SUP,
  606. .setting = 0x00
  607. }, {
  608. .abreg = AB3100_DIS,
  609. .setting = 0xF0
  610. }, {
  611. .abreg = AB3100_D0C,
  612. .setting = 0x00
  613. }, {
  614. .abreg = AB3100_D1C,
  615. .setting = 0x00
  616. }, {
  617. .abreg = AB3100_D2C,
  618. .setting = 0x00
  619. }, {
  620. .abreg = AB3100_D3C,
  621. .setting = 0x00
  622. },
  623. };
  624. static int __devinit ab3100_setup(struct ab3100 *ab3100)
  625. {
  626. int err = 0;
  627. int i;
  628. for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
  629. err = ab3100_set_register_interruptible(ab3100,
  630. ab3100_init_settings[i].abreg,
  631. ab3100_init_settings[i].setting);
  632. if (err)
  633. goto exit_no_setup;
  634. }
  635. /*
  636. * Special trick to make the AB3100 use the 32kHz clock (RTC)
  637. * bit 3 in test register 0x02 is a special, undocumented test
  638. * register bit that only exist in AB3100 P1E
  639. */
  640. if (ab3100->chip_id == 0xc4) {
  641. dev_warn(ab3100->dev,
  642. "AB3100 P1E variant detected, "
  643. "forcing chip to 32KHz\n");
  644. err = ab3100_set_test_register_interruptible(ab3100,
  645. 0x02, 0x08);
  646. }
  647. exit_no_setup:
  648. return err;
  649. }
  650. /* The subdevices of the AB3100 */
  651. static struct mfd_cell ab3100_devs[] = {
  652. {
  653. .name = "ab3100-dac",
  654. .id = -1,
  655. },
  656. {
  657. .name = "ab3100-leds",
  658. .id = -1,
  659. },
  660. {
  661. .name = "ab3100-power",
  662. .id = -1,
  663. },
  664. {
  665. .name = "ab3100-regulators",
  666. .id = -1,
  667. },
  668. {
  669. .name = "ab3100-sim",
  670. .id = -1,
  671. },
  672. {
  673. .name = "ab3100-uart",
  674. .id = -1,
  675. },
  676. {
  677. .name = "ab3100-rtc",
  678. .id = -1,
  679. },
  680. {
  681. .name = "ab3100-charger",
  682. .id = -1,
  683. },
  684. {
  685. .name = "ab3100-boost",
  686. .id = -1,
  687. },
  688. {
  689. .name = "ab3100-adc",
  690. .id = -1,
  691. },
  692. {
  693. .name = "ab3100-fuelgauge",
  694. .id = -1,
  695. },
  696. {
  697. .name = "ab3100-vibrator",
  698. .id = -1,
  699. },
  700. {
  701. .name = "ab3100-otp",
  702. .id = -1,
  703. },
  704. {
  705. .name = "ab3100-codec",
  706. .id = -1,
  707. },
  708. };
  709. struct ab_family_id {
  710. u8 id;
  711. char *name;
  712. };
  713. static const struct ab_family_id ids[] __devinitconst = {
  714. /* AB3100 */
  715. {
  716. .id = 0xc0,
  717. .name = "P1A"
  718. }, {
  719. .id = 0xc1,
  720. .name = "P1B"
  721. }, {
  722. .id = 0xc2,
  723. .name = "P1C"
  724. }, {
  725. .id = 0xc3,
  726. .name = "P1D"
  727. }, {
  728. .id = 0xc4,
  729. .name = "P1E"
  730. }, {
  731. .id = 0xc5,
  732. .name = "P1F/R1A"
  733. }, {
  734. .id = 0xc6,
  735. .name = "P1G/R1A"
  736. }, {
  737. .id = 0xc7,
  738. .name = "P2A/R2A"
  739. }, {
  740. .id = 0xc8,
  741. .name = "P2B/R2B"
  742. },
  743. /* AB3000 variants, not supported */
  744. {
  745. .id = 0xa0
  746. }, {
  747. .id = 0xa1
  748. }, {
  749. .id = 0xa2
  750. }, {
  751. .id = 0xa3
  752. }, {
  753. .id = 0xa4
  754. }, {
  755. .id = 0xa5
  756. }, {
  757. .id = 0xa6
  758. }, {
  759. .id = 0xa7
  760. },
  761. /* Terminator */
  762. {
  763. .id = 0x00,
  764. },
  765. };
  766. static int __devinit ab3100_probe(struct i2c_client *client,
  767. const struct i2c_device_id *id)
  768. {
  769. struct ab3100 *ab3100;
  770. struct ab3100_platform_data *ab3100_plf_data =
  771. client->dev.platform_data;
  772. int err;
  773. int i;
  774. ab3100 = devm_kzalloc(&client->dev, sizeof(struct ab3100), GFP_KERNEL);
  775. if (!ab3100) {
  776. dev_err(&client->dev, "could not allocate AB3100 device\n");
  777. return -ENOMEM;
  778. }
  779. /* Initialize data structure */
  780. mutex_init(&ab3100->access_mutex);
  781. BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
  782. ab3100->i2c_client = client;
  783. ab3100->dev = &ab3100->i2c_client->dev;
  784. i2c_set_clientdata(client, ab3100);
  785. /* Read chip ID register */
  786. err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
  787. &ab3100->chip_id);
  788. if (err) {
  789. dev_err(&client->dev,
  790. "could not communicate with the AB3100 analog "
  791. "baseband chip\n");
  792. goto exit_no_detect;
  793. }
  794. for (i = 0; ids[i].id != 0x0; i++) {
  795. if (ids[i].id == ab3100->chip_id) {
  796. if (ids[i].name != NULL) {
  797. snprintf(&ab3100->chip_name[0],
  798. sizeof(ab3100->chip_name) - 1,
  799. "AB3100 %s",
  800. ids[i].name);
  801. break;
  802. } else {
  803. dev_err(&client->dev,
  804. "AB3000 is not supported\n");
  805. goto exit_no_detect;
  806. }
  807. }
  808. }
  809. if (ids[i].id == 0x0) {
  810. dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
  811. ab3100->chip_id);
  812. dev_err(&client->dev, "accepting it anyway. Please update "
  813. "the driver.\n");
  814. goto exit_no_detect;
  815. }
  816. dev_info(&client->dev, "Detected chip: %s\n",
  817. &ab3100->chip_name[0]);
  818. /* Attach a second dummy i2c_client to the test register address */
  819. ab3100->testreg_client = i2c_new_dummy(client->adapter,
  820. client->addr + 1);
  821. if (!ab3100->testreg_client) {
  822. err = -ENOMEM;
  823. goto exit_no_testreg_client;
  824. }
  825. err = ab3100_setup(ab3100);
  826. if (err)
  827. goto exit_no_setup;
  828. err = devm_request_threaded_irq(&client->dev,
  829. client->irq, NULL, ab3100_irq_handler,
  830. IRQF_ONESHOT, "ab3100-core", ab3100);
  831. if (err)
  832. goto exit_no_irq;
  833. err = abx500_register_ops(&client->dev, &ab3100_ops);
  834. if (err)
  835. goto exit_no_ops;
  836. /* Set up and register the platform devices. */
  837. for (i = 0; i < ARRAY_SIZE(ab3100_devs); i++) {
  838. ab3100_devs[i].platform_data = ab3100_plf_data;
  839. ab3100_devs[i].pdata_size = sizeof(struct ab3100_platform_data);
  840. }
  841. err = mfd_add_devices(&client->dev, 0, ab3100_devs,
  842. ARRAY_SIZE(ab3100_devs), NULL, 0, NULL);
  843. ab3100_setup_debugfs(ab3100);
  844. return 0;
  845. exit_no_ops:
  846. exit_no_irq:
  847. exit_no_setup:
  848. i2c_unregister_device(ab3100->testreg_client);
  849. exit_no_testreg_client:
  850. exit_no_detect:
  851. return err;
  852. }
  853. static int __devexit ab3100_remove(struct i2c_client *client)
  854. {
  855. struct ab3100 *ab3100 = i2c_get_clientdata(client);
  856. /* Unregister subdevices */
  857. mfd_remove_devices(&client->dev);
  858. ab3100_remove_debugfs();
  859. i2c_unregister_device(ab3100->testreg_client);
  860. return 0;
  861. }
  862. static const struct i2c_device_id ab3100_id[] = {
  863. { "ab3100", 0 },
  864. { }
  865. };
  866. MODULE_DEVICE_TABLE(i2c, ab3100_id);
  867. static struct i2c_driver ab3100_driver = {
  868. .driver = {
  869. .name = "ab3100",
  870. .owner = THIS_MODULE,
  871. },
  872. .id_table = ab3100_id,
  873. .probe = ab3100_probe,
  874. .remove = __devexit_p(ab3100_remove),
  875. };
  876. static int __init ab3100_i2c_init(void)
  877. {
  878. return i2c_add_driver(&ab3100_driver);
  879. }
  880. static void __exit ab3100_i2c_exit(void)
  881. {
  882. i2c_del_driver(&ab3100_driver);
  883. }
  884. subsys_initcall(ab3100_i2c_init);
  885. module_exit(ab3100_i2c_exit);
  886. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  887. MODULE_DESCRIPTION("AB3100 core driver");
  888. MODULE_LICENSE("GPL");