si476x-i2c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * drivers/mfd/si476x-i2c.c -- Core device driver for si476x MFD
  3. * device
  4. *
  5. * Copyright (C) 2012 Innovative Converged Devices(ICD)
  6. * Copyright (C) 2013 Andrey Smirnov
  7. *
  8. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/gpio.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/i2c.h>
  27. #include <linux/err.h>
  28. #include <linux/mfd/si476x-core.h>
  29. #define SI476X_MAX_IO_ERRORS 10
  30. #define SI476X_DRIVER_RDS_FIFO_DEPTH 128
  31. /**
  32. * si476x_core_config_pinmux() - pin function configuration function
  33. *
  34. * @core: Core device structure
  35. *
  36. * Configure the functions of the pins of the radio chip.
  37. *
  38. * The function returns zero in case of succes or negative error code
  39. * otherwise.
  40. */
  41. static int si476x_core_config_pinmux(struct si476x_core *core)
  42. {
  43. int err;
  44. dev_dbg(&core->client->dev, "Configuring pinmux\n");
  45. err = si476x_core_cmd_dig_audio_pin_cfg(core,
  46. core->pinmux.dclk,
  47. core->pinmux.dfs,
  48. core->pinmux.dout,
  49. core->pinmux.xout);
  50. if (err < 0) {
  51. dev_err(&core->client->dev,
  52. "Failed to configure digital audio pins(err = %d)\n",
  53. err);
  54. return err;
  55. }
  56. err = si476x_core_cmd_zif_pin_cfg(core,
  57. core->pinmux.iqclk,
  58. core->pinmux.iqfs,
  59. core->pinmux.iout,
  60. core->pinmux.qout);
  61. if (err < 0) {
  62. dev_err(&core->client->dev,
  63. "Failed to configure ZIF pins(err = %d)\n",
  64. err);
  65. return err;
  66. }
  67. err = si476x_core_cmd_ic_link_gpo_ctl_pin_cfg(core,
  68. core->pinmux.icin,
  69. core->pinmux.icip,
  70. core->pinmux.icon,
  71. core->pinmux.icop);
  72. if (err < 0) {
  73. dev_err(&core->client->dev,
  74. "Failed to configure IC-Link/GPO pins(err = %d)\n",
  75. err);
  76. return err;
  77. }
  78. err = si476x_core_cmd_ana_audio_pin_cfg(core,
  79. core->pinmux.lrout);
  80. if (err < 0) {
  81. dev_err(&core->client->dev,
  82. "Failed to configure analog audio pins(err = %d)\n",
  83. err);
  84. return err;
  85. }
  86. err = si476x_core_cmd_intb_pin_cfg(core,
  87. core->pinmux.intb,
  88. core->pinmux.a1);
  89. if (err < 0) {
  90. dev_err(&core->client->dev,
  91. "Failed to configure interrupt pins(err = %d)\n",
  92. err);
  93. return err;
  94. }
  95. return 0;
  96. }
  97. static inline void si476x_core_schedule_polling_work(struct si476x_core *core)
  98. {
  99. schedule_delayed_work(&core->status_monitor,
  100. usecs_to_jiffies(SI476X_STATUS_POLL_US));
  101. }
  102. /**
  103. * si476x_core_start() - early chip startup function
  104. * @core: Core device structure
  105. * @soft: When set, this flag forces "soft" startup, where "soft"
  106. * power down is the one done by sending appropriate command instead
  107. * of using reset pin of the tuner
  108. *
  109. * Perform required startup sequence to correctly power
  110. * up the chip and perform initial configuration. It does the
  111. * following sequence of actions:
  112. * 1. Claims and enables the power supplies VD and VIO1 required
  113. * for I2C interface of the chip operation.
  114. * 2. Waits for 100us, pulls the reset line up, enables irq,
  115. * waits for another 100us as it is specified by the
  116. * datasheet.
  117. * 3. Sends 'POWER_UP' command to the device with all provided
  118. * information about power-up parameters.
  119. * 4. Configures, pin multiplexor, disables digital audio and
  120. * configures interrupt sources.
  121. *
  122. * The function returns zero in case of succes or negative error code
  123. * otherwise.
  124. */
  125. int si476x_core_start(struct si476x_core *core, bool soft)
  126. {
  127. struct i2c_client *client = core->client;
  128. int err;
  129. if (!soft) {
  130. if (gpio_is_valid(core->gpio_reset))
  131. gpio_set_value_cansleep(core->gpio_reset, 1);
  132. if (client->irq)
  133. enable_irq(client->irq);
  134. udelay(100);
  135. if (!client->irq) {
  136. atomic_set(&core->is_alive, 1);
  137. si476x_core_schedule_polling_work(core);
  138. }
  139. } else {
  140. if (client->irq)
  141. enable_irq(client->irq);
  142. else {
  143. atomic_set(&core->is_alive, 1);
  144. si476x_core_schedule_polling_work(core);
  145. }
  146. }
  147. err = si476x_core_cmd_power_up(core,
  148. &core->power_up_parameters);
  149. if (err < 0) {
  150. dev_err(&core->client->dev,
  151. "Power up failure(err = %d)\n",
  152. err);
  153. goto disable_irq;
  154. }
  155. if (client->irq)
  156. atomic_set(&core->is_alive, 1);
  157. err = si476x_core_config_pinmux(core);
  158. if (err < 0) {
  159. dev_err(&core->client->dev,
  160. "Failed to configure pinmux(err = %d)\n",
  161. err);
  162. goto disable_irq;
  163. }
  164. if (client->irq) {
  165. err = regmap_write(core->regmap,
  166. SI476X_PROP_INT_CTL_ENABLE,
  167. SI476X_RDSIEN |
  168. SI476X_STCIEN |
  169. SI476X_CTSIEN);
  170. if (err < 0) {
  171. dev_err(&core->client->dev,
  172. "Failed to configure interrupt sources"
  173. "(err = %d)\n", err);
  174. goto disable_irq;
  175. }
  176. }
  177. return 0;
  178. disable_irq:
  179. if (err == -ENODEV)
  180. atomic_set(&core->is_alive, 0);
  181. if (client->irq)
  182. disable_irq(client->irq);
  183. else
  184. cancel_delayed_work_sync(&core->status_monitor);
  185. if (gpio_is_valid(core->gpio_reset))
  186. gpio_set_value_cansleep(core->gpio_reset, 0);
  187. return err;
  188. }
  189. EXPORT_SYMBOL_GPL(si476x_core_start);
  190. /**
  191. * si476x_core_stop() - chip power-down function
  192. * @core: Core device structure
  193. * @soft: When set, function sends a POWER_DOWN command instead of
  194. * bringing reset line low
  195. *
  196. * Power down the chip by performing following actions:
  197. * 1. Disable IRQ or stop the polling worker
  198. * 2. Send the POWER_DOWN command if the power down is soft or bring
  199. * reset line low if not.
  200. *
  201. * The function returns zero in case of succes or negative error code
  202. * otherwise.
  203. */
  204. int si476x_core_stop(struct si476x_core *core, bool soft)
  205. {
  206. int err = 0;
  207. atomic_set(&core->is_alive, 0);
  208. if (soft) {
  209. /* TODO: This probably shoud be a configurable option,
  210. * so it is possible to have the chips keep their
  211. * oscillators running
  212. */
  213. struct si476x_power_down_args args = {
  214. .xosc = false,
  215. };
  216. err = si476x_core_cmd_power_down(core, &args);
  217. }
  218. /* We couldn't disable those before
  219. * 'si476x_core_cmd_power_down' since we expect to get CTS
  220. * interrupt */
  221. if (core->client->irq)
  222. disable_irq(core->client->irq);
  223. else
  224. cancel_delayed_work_sync(&core->status_monitor);
  225. if (!soft) {
  226. if (gpio_is_valid(core->gpio_reset))
  227. gpio_set_value_cansleep(core->gpio_reset, 0);
  228. }
  229. return err;
  230. }
  231. EXPORT_SYMBOL_GPL(si476x_core_stop);
  232. /**
  233. * si476x_core_set_power_state() - set the level at which the power is
  234. * supplied for the chip.
  235. * @core: Core device structure
  236. * @next_state: enum si476x_power_state describing power state to
  237. * switch to.
  238. *
  239. * Switch on all the required power supplies
  240. *
  241. * This function returns 0 in case of suvccess and negative error code
  242. * otherwise.
  243. */
  244. int si476x_core_set_power_state(struct si476x_core *core,
  245. enum si476x_power_state next_state)
  246. {
  247. /*
  248. It is not clear form the datasheet if it is possible to
  249. work with device if not all power domains are operational.
  250. So for now the power-up policy is "power-up all the things!"
  251. */
  252. int err = 0;
  253. if (core->power_state == SI476X_POWER_INCONSISTENT) {
  254. dev_err(&core->client->dev,
  255. "The device in inconsistent power state\n");
  256. return -EINVAL;
  257. }
  258. if (next_state != core->power_state) {
  259. switch (next_state) {
  260. case SI476X_POWER_UP_FULL:
  261. err = regulator_bulk_enable(ARRAY_SIZE(core->supplies),
  262. core->supplies);
  263. if (err < 0) {
  264. core->power_state = SI476X_POWER_INCONSISTENT;
  265. break;
  266. }
  267. /*
  268. * Startup timing diagram recommends to have a
  269. * 100 us delay between enabling of the power
  270. * supplies and turning the tuner on.
  271. */
  272. udelay(100);
  273. err = si476x_core_start(core, false);
  274. if (err < 0)
  275. goto disable_regulators;
  276. core->power_state = next_state;
  277. break;
  278. case SI476X_POWER_DOWN:
  279. core->power_state = next_state;
  280. err = si476x_core_stop(core, false);
  281. if (err < 0)
  282. core->power_state = SI476X_POWER_INCONSISTENT;
  283. disable_regulators:
  284. err = regulator_bulk_disable(ARRAY_SIZE(core->supplies),
  285. core->supplies);
  286. if (err < 0)
  287. core->power_state = SI476X_POWER_INCONSISTENT;
  288. break;
  289. default:
  290. BUG();
  291. }
  292. }
  293. return err;
  294. }
  295. EXPORT_SYMBOL_GPL(si476x_core_set_power_state);
  296. /**
  297. * si476x_core_report_drainer_stop() - mark the completion of the RDS
  298. * buffer drain porcess by the worker.
  299. *
  300. * @core: Core device structure
  301. */
  302. static inline void si476x_core_report_drainer_stop(struct si476x_core *core)
  303. {
  304. mutex_lock(&core->rds_drainer_status_lock);
  305. core->rds_drainer_is_working = false;
  306. mutex_unlock(&core->rds_drainer_status_lock);
  307. }
  308. /**
  309. * si476x_core_start_rds_drainer_once() - start RDS drainer worker if
  310. * ther is none working, do nothing otherwise
  311. *
  312. * @core: Datastructure corresponding to the chip.
  313. */
  314. static inline void si476x_core_start_rds_drainer_once(struct si476x_core *core)
  315. {
  316. mutex_lock(&core->rds_drainer_status_lock);
  317. if (!core->rds_drainer_is_working) {
  318. core->rds_drainer_is_working = true;
  319. schedule_work(&core->rds_fifo_drainer);
  320. }
  321. mutex_unlock(&core->rds_drainer_status_lock);
  322. }
  323. /**
  324. * si476x_drain_rds_fifo() - RDS buffer drainer.
  325. * @work: struct work_struct being ppassed to the function by the
  326. * kernel.
  327. *
  328. * Drain the contents of the RDS FIFO of
  329. */
  330. static void si476x_core_drain_rds_fifo(struct work_struct *work)
  331. {
  332. int err;
  333. struct si476x_core *core = container_of(work, struct si476x_core,
  334. rds_fifo_drainer);
  335. struct si476x_rds_status_report report;
  336. si476x_core_lock(core);
  337. err = si476x_core_cmd_fm_rds_status(core, true, false, false, &report);
  338. if (!err) {
  339. int i = report.rdsfifoused;
  340. dev_dbg(&core->client->dev,
  341. "%d elements in RDS FIFO. Draining.\n", i);
  342. for (; i > 0; --i) {
  343. err = si476x_core_cmd_fm_rds_status(core, false, false,
  344. (i == 1), &report);
  345. if (err < 0)
  346. goto unlock;
  347. kfifo_in(&core->rds_fifo, report.rds,
  348. sizeof(report.rds));
  349. dev_dbg(&core->client->dev, "RDS data:\n %*ph\n",
  350. (int)sizeof(report.rds), report.rds);
  351. }
  352. dev_dbg(&core->client->dev, "Drrrrained!\n");
  353. wake_up_interruptible(&core->rds_read_queue);
  354. }
  355. unlock:
  356. si476x_core_unlock(core);
  357. si476x_core_report_drainer_stop(core);
  358. }
  359. /**
  360. * si476x_core_pronounce_dead()
  361. *
  362. * @core: Core device structure
  363. *
  364. * Mark the device as being dead and wake up all potentially waiting
  365. * threads of execution.
  366. *
  367. */
  368. static void si476x_core_pronounce_dead(struct si476x_core *core)
  369. {
  370. dev_info(&core->client->dev, "Core device is dead.\n");
  371. atomic_set(&core->is_alive, 0);
  372. /* Wake up al possible waiting processes */
  373. wake_up_interruptible(&core->rds_read_queue);
  374. atomic_set(&core->cts, 1);
  375. wake_up(&core->command);
  376. atomic_set(&core->stc, 1);
  377. wake_up(&core->tuning);
  378. }
  379. /**
  380. * si476x_core_i2c_xfer()
  381. *
  382. * @core: Core device structure
  383. * @type: Transfer type
  384. * @buf: Transfer buffer for/with data
  385. * @count: Transfer buffer size
  386. *
  387. * Perfrom and I2C transfer(either read or write) and keep a counter
  388. * of I/O errors. If the error counter rises above the threshold
  389. * pronounce device dead.
  390. *
  391. * The function returns zero on succes or negative error code on
  392. * failure.
  393. */
  394. int si476x_core_i2c_xfer(struct si476x_core *core,
  395. enum si476x_i2c_type type,
  396. char *buf, int count)
  397. {
  398. static int io_errors_count;
  399. int err;
  400. if (type == SI476X_I2C_SEND)
  401. err = i2c_master_send(core->client, buf, count);
  402. else
  403. err = i2c_master_recv(core->client, buf, count);
  404. if (err < 0) {
  405. if (io_errors_count++ > SI476X_MAX_IO_ERRORS)
  406. si476x_core_pronounce_dead(core);
  407. } else {
  408. io_errors_count = 0;
  409. }
  410. return err;
  411. }
  412. EXPORT_SYMBOL_GPL(si476x_core_i2c_xfer);
  413. /**
  414. * si476x_get_status()
  415. * @core: Core device structure
  416. *
  417. * Get the status byte of the core device by berforming one byte I2C
  418. * read.
  419. *
  420. * The function returns a status value or a negative error code on
  421. * error.
  422. */
  423. static int si476x_core_get_status(struct si476x_core *core)
  424. {
  425. u8 response;
  426. int err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV,
  427. &response, sizeof(response));
  428. return (err < 0) ? err : response;
  429. }
  430. /**
  431. * si476x_get_and_signal_status() - IRQ dispatcher
  432. * @core: Core device structure
  433. *
  434. * Dispatch the arrived interrupt request based on the value of the
  435. * status byte reported by the tuner.
  436. *
  437. */
  438. static void si476x_core_get_and_signal_status(struct si476x_core *core)
  439. {
  440. int status = si476x_core_get_status(core);
  441. if (status < 0) {
  442. dev_err(&core->client->dev, "Failed to get status\n");
  443. return;
  444. }
  445. if (status & SI476X_CTS) {
  446. /* Unfortunately completions could not be used for
  447. * signalling CTS since this flag cannot be cleared
  448. * in status byte, and therefore once it becomes true
  449. * multiple calls to 'complete' would cause the
  450. * commands following the current one to be completed
  451. * before they actually are */
  452. dev_dbg(&core->client->dev, "[interrupt] CTSINT\n");
  453. atomic_set(&core->cts, 1);
  454. wake_up(&core->command);
  455. }
  456. if (status & SI476X_FM_RDS_INT) {
  457. dev_dbg(&core->client->dev, "[interrupt] RDSINT\n");
  458. si476x_core_start_rds_drainer_once(core);
  459. }
  460. if (status & SI476X_STC_INT) {
  461. dev_dbg(&core->client->dev, "[interrupt] STCINT\n");
  462. atomic_set(&core->stc, 1);
  463. wake_up(&core->tuning);
  464. }
  465. }
  466. static void si476x_core_poll_loop(struct work_struct *work)
  467. {
  468. struct si476x_core *core = SI476X_WORK_TO_CORE(work);
  469. si476x_core_get_and_signal_status(core);
  470. if (atomic_read(&core->is_alive))
  471. si476x_core_schedule_polling_work(core);
  472. }
  473. static irqreturn_t si476x_core_interrupt(int irq, void *dev)
  474. {
  475. struct si476x_core *core = dev;
  476. si476x_core_get_and_signal_status(core);
  477. return IRQ_HANDLED;
  478. }
  479. /**
  480. * si476x_firmware_version_to_revision()
  481. * @core: Core device structure
  482. * @major: Firmware major number
  483. * @minor1: Firmware first minor number
  484. * @minor2: Firmware second minor number
  485. *
  486. * Convert a chip's firmware version number into an offset that later
  487. * will be used to as offset in "vtable" of tuner functions
  488. *
  489. * This function returns a positive offset in case of success and a -1
  490. * in case of failure.
  491. */
  492. static int si476x_core_fwver_to_revision(struct si476x_core *core,
  493. int func, int major,
  494. int minor1, int minor2)
  495. {
  496. switch (func) {
  497. case SI476X_FUNC_FM_RECEIVER:
  498. switch (major) {
  499. case 5:
  500. return SI476X_REVISION_A10;
  501. case 8:
  502. return SI476X_REVISION_A20;
  503. case 10:
  504. return SI476X_REVISION_A30;
  505. default:
  506. goto unknown_revision;
  507. }
  508. case SI476X_FUNC_AM_RECEIVER:
  509. switch (major) {
  510. case 5:
  511. return SI476X_REVISION_A10;
  512. case 7:
  513. return SI476X_REVISION_A20;
  514. case 9:
  515. return SI476X_REVISION_A30;
  516. default:
  517. goto unknown_revision;
  518. }
  519. case SI476X_FUNC_WB_RECEIVER:
  520. switch (major) {
  521. case 3:
  522. return SI476X_REVISION_A10;
  523. case 5:
  524. return SI476X_REVISION_A20;
  525. case 7:
  526. return SI476X_REVISION_A30;
  527. default:
  528. goto unknown_revision;
  529. }
  530. case SI476X_FUNC_BOOTLOADER:
  531. default: /* FALLTHROUG */
  532. BUG();
  533. return -1;
  534. }
  535. unknown_revision:
  536. dev_err(&core->client->dev,
  537. "Unsupported version of the firmware: %d.%d.%d, "
  538. "reverting to A10 comptible functions\n",
  539. major, minor1, minor2);
  540. return SI476X_REVISION_A10;
  541. }
  542. /**
  543. * si476x_get_revision_info()
  544. * @core: Core device structure
  545. *
  546. * Get the firmware version number of the device. It is done in
  547. * following three steps:
  548. * 1. Power-up the device
  549. * 2. Send the 'FUNC_INFO' command
  550. * 3. Powering the device down.
  551. *
  552. * The function return zero on success and a negative error code on
  553. * failure.
  554. */
  555. static int si476x_core_get_revision_info(struct si476x_core *core)
  556. {
  557. int rval;
  558. struct si476x_func_info info;
  559. si476x_core_lock(core);
  560. rval = si476x_core_set_power_state(core, SI476X_POWER_UP_FULL);
  561. if (rval < 0)
  562. goto exit;
  563. rval = si476x_core_cmd_func_info(core, &info);
  564. if (rval < 0)
  565. goto power_down;
  566. core->revision = si476x_core_fwver_to_revision(core, info.func,
  567. info.firmware.major,
  568. info.firmware.minor[0],
  569. info.firmware.minor[1]);
  570. power_down:
  571. si476x_core_set_power_state(core, SI476X_POWER_DOWN);
  572. exit:
  573. si476x_core_unlock(core);
  574. return rval;
  575. }
  576. bool si476x_core_has_am(struct si476x_core *core)
  577. {
  578. return core->chip_id == SI476X_CHIP_SI4761 ||
  579. core->chip_id == SI476X_CHIP_SI4764;
  580. }
  581. EXPORT_SYMBOL_GPL(si476x_core_has_am);
  582. bool si476x_core_has_diversity(struct si476x_core *core)
  583. {
  584. return core->chip_id == SI476X_CHIP_SI4764;
  585. }
  586. EXPORT_SYMBOL_GPL(si476x_core_has_diversity);
  587. bool si476x_core_is_a_secondary_tuner(struct si476x_core *core)
  588. {
  589. return si476x_core_has_diversity(core) &&
  590. (core->diversity_mode == SI476X_PHDIV_SECONDARY_ANTENNA ||
  591. core->diversity_mode == SI476X_PHDIV_SECONDARY_COMBINING);
  592. }
  593. EXPORT_SYMBOL_GPL(si476x_core_is_a_secondary_tuner);
  594. bool si476x_core_is_a_primary_tuner(struct si476x_core *core)
  595. {
  596. return si476x_core_has_diversity(core) &&
  597. (core->diversity_mode == SI476X_PHDIV_PRIMARY_ANTENNA ||
  598. core->diversity_mode == SI476X_PHDIV_PRIMARY_COMBINING);
  599. }
  600. EXPORT_SYMBOL_GPL(si476x_core_is_a_primary_tuner);
  601. bool si476x_core_is_in_am_receiver_mode(struct si476x_core *core)
  602. {
  603. return si476x_core_has_am(core) &&
  604. (core->power_up_parameters.func == SI476X_FUNC_AM_RECEIVER);
  605. }
  606. EXPORT_SYMBOL_GPL(si476x_core_is_in_am_receiver_mode);
  607. bool si476x_core_is_powered_up(struct si476x_core *core)
  608. {
  609. return core->power_state == SI476X_POWER_UP_FULL;
  610. }
  611. EXPORT_SYMBOL_GPL(si476x_core_is_powered_up);
  612. static int si476x_core_probe(struct i2c_client *client,
  613. const struct i2c_device_id *id)
  614. {
  615. int rval;
  616. struct si476x_core *core;
  617. struct si476x_platform_data *pdata;
  618. struct mfd_cell *cell;
  619. int cell_num;
  620. core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
  621. if (!core) {
  622. dev_err(&client->dev,
  623. "failed to allocate 'struct si476x_core'\n");
  624. return -ENOMEM;
  625. }
  626. core->client = client;
  627. core->regmap = devm_regmap_init_si476x(core);
  628. if (IS_ERR(core->regmap)) {
  629. rval = PTR_ERR(core->regmap);
  630. dev_err(&client->dev,
  631. "Failed to allocate register map: %d\n",
  632. rval);
  633. return rval;
  634. }
  635. i2c_set_clientdata(client, core);
  636. atomic_set(&core->is_alive, 0);
  637. core->power_state = SI476X_POWER_DOWN;
  638. pdata = dev_get_platdata(&client->dev);
  639. if (pdata) {
  640. memcpy(&core->power_up_parameters,
  641. &pdata->power_up_parameters,
  642. sizeof(core->power_up_parameters));
  643. core->gpio_reset = -1;
  644. if (gpio_is_valid(pdata->gpio_reset)) {
  645. rval = gpio_request(pdata->gpio_reset, "si476x reset");
  646. if (rval) {
  647. dev_err(&client->dev,
  648. "Failed to request gpio: %d\n", rval);
  649. return rval;
  650. }
  651. core->gpio_reset = pdata->gpio_reset;
  652. gpio_direction_output(core->gpio_reset, 0);
  653. }
  654. core->diversity_mode = pdata->diversity_mode;
  655. memcpy(&core->pinmux, &pdata->pinmux,
  656. sizeof(struct si476x_pinmux));
  657. } else {
  658. dev_err(&client->dev, "No platform data provided\n");
  659. return -EINVAL;
  660. }
  661. core->supplies[0].supply = "vd";
  662. core->supplies[1].supply = "va";
  663. core->supplies[2].supply = "vio1";
  664. core->supplies[3].supply = "vio2";
  665. rval = devm_regulator_bulk_get(&client->dev,
  666. ARRAY_SIZE(core->supplies),
  667. core->supplies);
  668. if (rval) {
  669. dev_err(&client->dev, "Failet to gett all of the regulators\n");
  670. goto free_gpio;
  671. }
  672. mutex_init(&core->cmd_lock);
  673. init_waitqueue_head(&core->command);
  674. init_waitqueue_head(&core->tuning);
  675. rval = kfifo_alloc(&core->rds_fifo,
  676. SI476X_DRIVER_RDS_FIFO_DEPTH *
  677. sizeof(struct v4l2_rds_data),
  678. GFP_KERNEL);
  679. if (rval) {
  680. dev_err(&client->dev, "Could not allocate the FIFO\n");
  681. goto free_gpio;
  682. }
  683. mutex_init(&core->rds_drainer_status_lock);
  684. init_waitqueue_head(&core->rds_read_queue);
  685. INIT_WORK(&core->rds_fifo_drainer, si476x_core_drain_rds_fifo);
  686. if (client->irq) {
  687. rval = devm_request_threaded_irq(&client->dev,
  688. client->irq, NULL,
  689. si476x_core_interrupt,
  690. IRQF_TRIGGER_FALLING |
  691. IRQF_ONESHOT,
  692. client->name, core);
  693. if (rval < 0) {
  694. dev_err(&client->dev, "Could not request IRQ %d\n",
  695. client->irq);
  696. goto free_kfifo;
  697. }
  698. disable_irq(client->irq);
  699. dev_dbg(&client->dev, "IRQ requested.\n");
  700. core->rds_fifo_depth = 20;
  701. } else {
  702. INIT_DELAYED_WORK(&core->status_monitor,
  703. si476x_core_poll_loop);
  704. dev_info(&client->dev,
  705. "No IRQ number specified, will use polling\n");
  706. core->rds_fifo_depth = 5;
  707. }
  708. core->chip_id = id->driver_data;
  709. rval = si476x_core_get_revision_info(core);
  710. if (rval < 0) {
  711. rval = -ENODEV;
  712. goto free_kfifo;
  713. }
  714. cell_num = 0;
  715. cell = &core->cells[SI476X_RADIO_CELL];
  716. cell->name = "si476x-radio";
  717. cell_num++;
  718. #ifdef CONFIG_SND_SOC_SI476X
  719. if ((core->chip_id == SI476X_CHIP_SI4761 ||
  720. core->chip_id == SI476X_CHIP_SI4764) &&
  721. core->pinmux.dclk == SI476X_DCLK_DAUDIO &&
  722. core->pinmux.dfs == SI476X_DFS_DAUDIO &&
  723. core->pinmux.dout == SI476X_DOUT_I2S_OUTPUT &&
  724. core->pinmux.xout == SI476X_XOUT_TRISTATE) {
  725. cell = &core->cells[SI476X_CODEC_CELL];
  726. cell->name = "si476x-codec";
  727. cell_num++;
  728. }
  729. #endif
  730. rval = mfd_add_devices(&client->dev,
  731. (client->adapter->nr << 8) + client->addr,
  732. core->cells, cell_num,
  733. NULL, 0, NULL);
  734. if (!rval)
  735. return 0;
  736. free_kfifo:
  737. kfifo_free(&core->rds_fifo);
  738. free_gpio:
  739. if (gpio_is_valid(core->gpio_reset))
  740. gpio_free(core->gpio_reset);
  741. return rval;
  742. }
  743. static int si476x_core_remove(struct i2c_client *client)
  744. {
  745. struct si476x_core *core = i2c_get_clientdata(client);
  746. si476x_core_pronounce_dead(core);
  747. mfd_remove_devices(&client->dev);
  748. if (client->irq)
  749. disable_irq(client->irq);
  750. else
  751. cancel_delayed_work_sync(&core->status_monitor);
  752. kfifo_free(&core->rds_fifo);
  753. if (gpio_is_valid(core->gpio_reset))
  754. gpio_free(core->gpio_reset);
  755. return 0;
  756. }
  757. static const struct i2c_device_id si476x_id[] = {
  758. { "si4761", SI476X_CHIP_SI4761 },
  759. { "si4764", SI476X_CHIP_SI4764 },
  760. { "si4768", SI476X_CHIP_SI4768 },
  761. { },
  762. };
  763. MODULE_DEVICE_TABLE(i2c, si476x_id);
  764. static struct i2c_driver si476x_core_driver = {
  765. .driver = {
  766. .name = "si476x-core",
  767. },
  768. .probe = si476x_core_probe,
  769. .remove = si476x_core_remove,
  770. .id_table = si476x_id,
  771. };
  772. module_i2c_driver(si476x_core_driver);
  773. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  774. MODULE_DESCRIPTION("Si4761/64/68 AM/FM MFD core device driver");
  775. MODULE_LICENSE("GPL");