ab3100-core.c 23 KB

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