ab3100-core.c 22 KB

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