ab3100-core.c 22 KB

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