radio-rtc6226-i2c.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /* drivers/media/radio/rtc6226/radio-rtc6226-i2c.c
  2. *
  3. * Driver for Richwave RTC6226 FM Tuner
  4. *
  5. * Copyright (c) 2009 Samsung Electronics Co.Ltd
  6. * Author: Joonyoung Shim <[email protected]>
  7. * Copyright (c) 2009 Tobias Lorenz <[email protected]>
  8. * Copyright (c) 2012 Hans de Goede <[email protected]>
  9. * Copyright (c) 2018 LG Electronics, Inc.
  10. * Copyright (c) 2018 Richwave Technology Co.Ltd
  11. * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /* kernel includes */
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/of_gpio.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/regulator/consumer.h>
  36. #include "radio-rtc6226.h"
  37. #include <linux/workqueue.h>
  38. static const struct of_device_id rtc6226_i2c_dt_ids[] = {
  39. {.compatible = "rtc6226"},
  40. {}
  41. };
  42. /* I2C Device ID List */
  43. static const struct i2c_device_id rtc6226_i2c_id[] = {
  44. /* Generic Entry */
  45. { "rtc6226", 0 },
  46. /* Terminating entry */
  47. { }
  48. };
  49. MODULE_DEVICE_TABLE(i2c, rtc6226_i2c_id);
  50. /**************************************************************************
  51. * Module Parameters
  52. **************************************************************************/
  53. /* Radio Nr */
  54. static int radio_nr = -1;
  55. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  56. /* RDS buffer blocks */
  57. static unsigned int rds_buf = 100;
  58. MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
  59. enum rtc6226_ctrl_id {
  60. RTC6226_ID_CSR0_ENABLE,
  61. RTC6226_ID_CSR0_DISABLE,
  62. RTC6226_ID_DEVICEID,
  63. RTC6226_ID_CSR0_DIS_SMUTE,
  64. RTC6226_ID_CSR0_DIS_MUTE,
  65. RTC6226_ID_CSR0_DEEM,
  66. RTC6226_ID_CSR0_BLNDADJUST,
  67. RTC6226_ID_CSR0_VOLUME,
  68. RTC6226_ID_CSR0_BAND,
  69. RTC6226_ID_CSR0_CHSPACE,
  70. RTC6226_ID_CSR0_DIS_AGC,
  71. RTC6226_ID_CSR0_RDS_EN,
  72. RTC6226_ID_SEEK_CANCEL,
  73. RTC6226_ID_CSR0_SEEKRSSITH,
  74. RTC6226_ID_CSR0_OFSTH,
  75. RTC6226_ID_CSR0_QLTTH,
  76. RTC6226_ID_RSSI,
  77. RTC6226_ID_RDS_RDY,
  78. RTC6226_ID_STD,
  79. RTC6226_ID_SF,
  80. RTC6226_ID_RDS_SYNC,
  81. RTC6226_ID_SI,
  82. };
  83. /**************************************************************************
  84. * I2C Definitions
  85. **************************************************************************/
  86. /* Write starts with the upper byte of register 0x02 */
  87. #define WRITE_REG_NUM 3
  88. #define WRITE_INDEX(i) ((i + 0x02)%16)
  89. /* Read starts with the upper byte of register 0x0a */
  90. #define READ_REG_NUM 2
  91. #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
  92. /*static*/
  93. struct tasklet_struct my_tasklet;
  94. /*
  95. * rtc6226_get_register - read register
  96. */
  97. int rtc6226_get_register(struct rtc6226_device *radio, int regnr)
  98. {
  99. u8 reg[1];
  100. u8 buf[READ_REG_NUM];
  101. struct i2c_msg msgs[2] = {
  102. { radio->client->addr, 0, 1, reg },
  103. { radio->client->addr, I2C_M_RD, sizeof(buf), buf },
  104. };
  105. reg[0] = (u8)(regnr);
  106. if (i2c_transfer(radio->client->adapter, msgs, 2) != 2)
  107. return -EIO;
  108. radio->registers[regnr] =
  109. (u16)(((buf[0] << 8) & 0xff00) | buf[1]);
  110. return 0;
  111. }
  112. /*
  113. * rtc6226_set_register - write register
  114. */
  115. int rtc6226_set_register(struct rtc6226_device *radio, int regnr)
  116. {
  117. u8 buf[WRITE_REG_NUM];
  118. struct i2c_msg msgs[1] = {
  119. { radio->client->addr, 0, sizeof(u8) * WRITE_REG_NUM,
  120. (void *)buf },
  121. };
  122. buf[0] = (u8)(regnr);
  123. buf[1] = (u8)((radio->registers[(u8)(regnr) & 0xFF] >> 8) & 0xFF);
  124. buf[2] = (u8)(radio->registers[(u8)(regnr) & 0xFF] & 0xFF);
  125. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  126. return -EIO;
  127. return 0;
  128. }
  129. /*
  130. * rtc6226_set_register - write register
  131. */
  132. int rtc6226_set_serial_registers(struct rtc6226_device *radio,
  133. u16 *data, int regnr)
  134. {
  135. u8 buf[WRITE_REG_NUM];
  136. struct i2c_msg msgs[1] = {
  137. { radio->client->addr, 0, sizeof(u8) * WRITE_REG_NUM,
  138. (void *)buf },
  139. };
  140. buf[0] = (u8)(regnr);
  141. buf[1] = (u8)((data[0] >> 8) & 0xFF);
  142. buf[2] = (u8)(data[0] & 0xFF);
  143. if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
  144. return -EIO;
  145. return 0;
  146. }
  147. /**************************************************************************
  148. * General Driver Functions - ENTIRE REGISTERS
  149. **************************************************************************/
  150. /*
  151. * rtc6226_get_all_registers - read entire registers
  152. */
  153. /* changed from static */
  154. int rtc6226_get_all_registers(struct rtc6226_device *radio)
  155. {
  156. int i;
  157. int err;
  158. u8 reg[1] = {0x00};
  159. u8 buf[RADIO_REGISTER_NUM];
  160. struct i2c_msg msgs1[1] = {
  161. { radio->client->addr, 0, 1, reg},
  162. };
  163. struct i2c_msg msgs[1] = {
  164. { radio->client->addr, I2C_M_RD, sizeof(buf), buf },
  165. };
  166. if (i2c_transfer(radio->client->adapter, msgs1, 1) != 1)
  167. return -EIO;
  168. err = i2c_transfer(radio->client->adapter, msgs, 1);
  169. if (err < 0)
  170. return -EIO;
  171. for (i = 0; i < 16; i++)
  172. radio->registers[i] =
  173. (u16)(((buf[i*2] << 8) & 0xff00) | buf[i*2+1]);
  174. return 0;
  175. }
  176. /*
  177. * rtc6226_vidioc_querycap - query device capabilities
  178. */
  179. int rtc6226_vidioc_querycap(struct file *file, void *priv,
  180. struct v4l2_capability *capability)
  181. {
  182. FMDBG("%s enter\n", __func__);
  183. strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
  184. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  185. capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
  186. V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  187. capability->capabilities = capability->device_caps |
  188. V4L2_CAP_DEVICE_CAPS;
  189. return 0;
  190. }
  191. /*
  192. * rtc6226_i2c_interrupt - interrupt handler
  193. */
  194. static void rtc6226_i2c_interrupt_handler(struct rtc6226_device *radio)
  195. {
  196. unsigned char regnr;
  197. int retval = 0;
  198. unsigned short current_chan;
  199. FMDBG("%s enter\n", __func__);
  200. /* check Seek/Tune Complete */
  201. retval = rtc6226_get_register(radio, STATUS);
  202. if (retval < 0) {
  203. FMDERR("%s read fail to STATUS\n", __func__);
  204. goto end;
  205. }
  206. if (radio->registers[STATUS] & STATUS_STD) {
  207. FMDBG("%s : STATUS=0x%4.4hx\n", __func__,
  208. radio->registers[STATUS]);
  209. retval = rtc6226_get_register(radio, RSSI);
  210. if (retval < 0) {
  211. FMDERR("%s read fail to RSSI\n", __func__);
  212. goto end;
  213. }
  214. FMDBG("%s : RSSI=0x%4.4hx\n", __func__, radio->registers[RSSI]);
  215. /* stop seeking : clear STD*/
  216. radio->registers[SEEKCFG1] &= ~SEEKCFG1_CSR0_SEEK;
  217. retval = rtc6226_set_register(radio, SEEKCFG1);
  218. /*clear the status bit to allow another tune or seek*/
  219. current_chan = radio->registers[CHANNEL] & CHANNEL_CSR0_CH;
  220. radio->registers[CHANNEL] &= ~CHANNEL_CSR0_TUNE;
  221. retval = rtc6226_set_register(radio, CHANNEL);
  222. if (retval < 0)
  223. radio->registers[CHANNEL] = current_chan;
  224. rtc6226_reset_rds_data(radio);
  225. FMDBG("%s clear Seek/Tune bit\n", __func__);
  226. if (radio->seek_tune_status == SEEK_PENDING) {
  227. /* Enable the RDS as it was disabled before seek */
  228. rtc6226_rds_on(radio);
  229. FMDBG("posting RTC6226_EVT_SEEK_COMPLETE event\n");
  230. rtc6226_q_event(radio, RTC6226_EVT_SEEK_COMPLETE);
  231. /* post tune comp evt since seek results in a tune.*/
  232. FMDBG("posting RICHWAVE_EVT_TUNE_SUCC event\n");
  233. rtc6226_q_event(radio, RTC6226_EVT_TUNE_SUCC);
  234. radio->seek_tune_status = NO_SEEK_TUNE_PENDING;
  235. } else if (radio->seek_tune_status == TUNE_PENDING) {
  236. FMDBG("posting RICHWAVE_EVT_TUNE_SUCC event\n");
  237. rtc6226_q_event(radio, RTC6226_EVT_TUNE_SUCC);
  238. radio->seek_tune_status = NO_SEEK_TUNE_PENDING;
  239. } else if (radio->seek_tune_status == SCAN_PENDING) {
  240. /* when scan is pending and STC int is set, signal
  241. * so that scan can proceed
  242. */
  243. FMDBG("In %s, signalling scan thread\n", __func__);
  244. complete(&radio->completion);
  245. }
  246. FMDBG("%s Seek/Tune done\n", __func__);
  247. } else {
  248. /* Check RDS data after tune/seek interrupt finished
  249. * Update RDS registers
  250. */
  251. for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
  252. retval = rtc6226_get_register(radio, STATUS + regnr);
  253. if (retval < 0)
  254. goto end;
  255. }
  256. /* get rds blocks */
  257. if ((radio->registers[STATUS] & STATUS_RDS_RDY) == 0) {
  258. /* No RDS group ready, better luck next time */
  259. FMDERR("%s No RDS group ready\n", __func__);
  260. goto end;
  261. } else {
  262. /* avoid RDS interrupt lock disable_irq*/
  263. if ((radio->registers[SYSCFG] &
  264. SYSCFG_CSR0_RDS_EN) != 0) {
  265. schedule_work(&radio->rds_worker);
  266. }
  267. }
  268. }
  269. end:
  270. FMDBG("%s exit :%d\n", __func__, retval);
  271. }
  272. static irqreturn_t rtc6226_isr(int irq, void *dev_id)
  273. {
  274. struct rtc6226_device *radio = dev_id;
  275. /*
  276. * The call to queue_delayed_work ensures that a minimum delay
  277. * (in jiffies) passes before the work is actually executed. The return
  278. * value from the function is nonzero if the work_struct was actually
  279. * added to queue (otherwise, it may have already been there and will
  280. * not be added a second time).
  281. */
  282. queue_delayed_work(radio->wqueue, &radio->work,
  283. msecs_to_jiffies(10));
  284. return IRQ_HANDLED;
  285. }
  286. static void rtc6226_handler(struct work_struct *work)
  287. {
  288. struct rtc6226_device *radio;
  289. radio = container_of(work, struct rtc6226_device, work.work);
  290. rtc6226_i2c_interrupt_handler(radio);
  291. }
  292. void rtc6226_disable_irq(struct rtc6226_device *radio)
  293. {
  294. int irq;
  295. irq = radio->irq;
  296. disable_irq_wake(irq);
  297. free_irq(irq, radio);
  298. cancel_delayed_work_sync(&radio->work);
  299. flush_workqueue(radio->wqueue);
  300. cancel_work_sync(&radio->rds_worker);
  301. flush_workqueue(radio->wqueue_rds);
  302. cancel_delayed_work_sync(&radio->work_scan);
  303. flush_workqueue(radio->wqueue_scan);
  304. }
  305. int rtc6226_enable_irq(struct rtc6226_device *radio)
  306. {
  307. int retval;
  308. int irq;
  309. retval = gpio_direction_input(radio->int_gpio);
  310. if (retval) {
  311. FMDERR("%s unable to set the gpio %d direction(%d)\n",
  312. __func__, radio->int_gpio, retval);
  313. return retval;
  314. }
  315. radio->irq = gpio_to_irq(radio->int_gpio);
  316. irq = radio->irq;
  317. if (radio->irq < 0) {
  318. FMDERR("%s: gpio_to_irq returned %d\n", __func__, radio->irq);
  319. goto open_err_req_irq;
  320. }
  321. FMDBG("%s irq number is = %d\n", __func__, radio->irq);
  322. retval = request_any_context_irq(radio->irq, rtc6226_isr,
  323. IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
  324. if (retval < 0) {
  325. FMDERR("%s Couldn't acquire FM gpio %d, retval:%d\n",
  326. __func__, radio->irq, retval);
  327. goto open_err_req_irq;
  328. } else {
  329. FMDBG("%s FM GPIO %d registered\n", __func__, radio->irq);
  330. }
  331. retval = enable_irq_wake(irq);
  332. if (retval < 0) {
  333. FMDERR("Could not wake FM interrupt\n");
  334. free_irq(irq, radio);
  335. }
  336. return retval;
  337. open_err_req_irq:
  338. rtc6226_disable_irq(radio);
  339. return retval;
  340. }
  341. static int rtc6226_fm_vio_reg_cfg(struct rtc6226_device *radio, bool on)
  342. {
  343. int rc = 0;
  344. struct fm_power_vreg_data *vreg;
  345. vreg = radio->vioreg;
  346. if (!vreg) {
  347. FMDERR("In %s, vio reg is NULL\n", __func__);
  348. return rc;
  349. }
  350. if (on) {
  351. FMDBG("vreg is : %s\n", vreg->name);
  352. rc = regulator_set_voltage(vreg->reg,
  353. vreg->low_vol_level,
  354. vreg->high_vol_level);
  355. if (rc < 0) {
  356. FMDERR("set_vol(%s) fail %d\n", vreg->name, rc);
  357. return rc;
  358. }
  359. rc = regulator_enable(vreg->reg);
  360. if (rc < 0) {
  361. FMDERR("reg enable(%s) failed.rc=%d\n", vreg->name, rc);
  362. regulator_set_voltage(vreg->reg,
  363. 0,
  364. vreg->high_vol_level);
  365. return rc;
  366. }
  367. vreg->is_enabled = true;
  368. } else {
  369. rc = regulator_disable(vreg->reg);
  370. if (rc < 0) {
  371. FMDERR("reg disable(%s) fail rc=%d\n", vreg->name, rc);
  372. return rc;
  373. }
  374. vreg->is_enabled = false;
  375. /* Set the min voltage to 0 */
  376. rc = regulator_set_voltage(vreg->reg,
  377. 0,
  378. vreg->high_vol_level);
  379. if (rc < 0) {
  380. FMDERR("set_vol(%s) fail %d\n", vreg->name, rc);
  381. return rc;
  382. }
  383. }
  384. return rc;
  385. }
  386. static int rtc6226_fm_vdd_reg_cfg(struct rtc6226_device *radio, bool on)
  387. {
  388. int rc = 0;
  389. struct fm_power_vreg_data *vreg;
  390. vreg = radio->vddreg;
  391. if (!vreg) {
  392. FMDERR("In %s, vdd reg is NULL\n", __func__);
  393. return rc;
  394. }
  395. if (on) {
  396. FMDBG("vreg is : %s\n", vreg->name);
  397. rc = regulator_set_voltage(vreg->reg,
  398. vreg->low_vol_level,
  399. vreg->high_vol_level);
  400. if (rc < 0) {
  401. FMDERR("set_vol(%s) fail %d\n", vreg->name, rc);
  402. return rc;
  403. }
  404. if (vreg->vdd_load) {
  405. rc = regulator_set_load(vreg->reg, vreg->vdd_load);
  406. if (rc < 0) {
  407. FMDERR("%s Unable to set the load %d ,err=%d\n",
  408. __func__, vreg->vdd_load, rc);
  409. return rc;
  410. }
  411. }
  412. rc = regulator_enable(vreg->reg);
  413. if (rc < 0) {
  414. FMDERR("reg enable(%s) failed.rc=%d\n", vreg->name, rc);
  415. regulator_set_voltage(vreg->reg,
  416. 0,
  417. vreg->high_vol_level);
  418. return rc;
  419. }
  420. vreg->is_enabled = true;
  421. } else {
  422. rc = regulator_disable(vreg->reg);
  423. if (rc < 0) {
  424. FMDERR("reg disable(%s) fail. rc=%d\n", vreg->name, rc);
  425. return rc;
  426. }
  427. vreg->is_enabled = false;
  428. /* Set the min voltage to 0 */
  429. rc = regulator_set_voltage(vreg->reg,
  430. 0,
  431. vreg->high_vol_level);
  432. if (rc < 0) {
  433. FMDERR("set_vol(%s) fail %d\n", vreg->name, rc);
  434. return rc;
  435. }
  436. if (vreg->vdd_load) {
  437. rc = regulator_set_load(vreg->reg, 0);
  438. if (rc < 0) {
  439. FMDERR("%s Unable to set the load 0 ,err=%d\n",
  440. __func__, rc);
  441. return rc;
  442. }
  443. }
  444. }
  445. return rc;
  446. }
  447. static int rtc6226_fm_power_cfg(struct rtc6226_device *radio, bool powerflag)
  448. {
  449. int rc = 0;
  450. if (powerflag) {
  451. /* Turn ON sequence */
  452. rc = rtc6226_fm_vdd_reg_cfg(radio, powerflag);
  453. if (rc < 0) {
  454. FMDERR("In %s, vdd reg cfg failed %x\n", __func__, rc);
  455. return rc;
  456. }
  457. rc = rtc6226_fm_vio_reg_cfg(radio, powerflag);
  458. if (rc < 0) {
  459. FMDERR("In %s, vio reg cfg failed %x\n", __func__, rc);
  460. rtc6226_fm_vdd_reg_cfg(radio, false);
  461. return rc;
  462. }
  463. } else {
  464. /* Turn OFF sequence */
  465. rc = rtc6226_fm_vdd_reg_cfg(radio, powerflag);
  466. if (rc < 0)
  467. FMDERR("In %s, vdd reg cfg failed %x\n", __func__, rc);
  468. rc = rtc6226_fm_vio_reg_cfg(radio, powerflag);
  469. if (rc < 0)
  470. FMDERR("In %s, vio reg cfg failed %x\n", __func__, rc);
  471. }
  472. return rc;
  473. }
  474. /*
  475. * rtc6226_fops_open - file open
  476. */
  477. int rtc6226_fops_open(struct file *file)
  478. {
  479. struct rtc6226_device *radio = video_drvdata(file);
  480. int retval;
  481. FMDBG("%s enter user num = %d\n", __func__, radio->users);
  482. if (atomic_inc_return(&radio->users) != 1) {
  483. FMDERR("Device already in use. Try again later\n");
  484. atomic_dec(&radio->users);
  485. return -EBUSY;
  486. }
  487. INIT_DELAYED_WORK(&radio->work, rtc6226_handler);
  488. INIT_DELAYED_WORK(&radio->work_scan, rtc6226_scan);
  489. INIT_WORK(&radio->rds_worker, rtc6226_rds_handler);
  490. /* Power up Supply voltage to VDD and VIO */
  491. retval = rtc6226_fm_power_cfg(radio, TURNING_ON);
  492. if (retval) {
  493. FMDERR("%s: failed to supply voltage\n", __func__);
  494. goto open_err_setup;
  495. }
  496. retval = rtc6226_enable_irq(radio);
  497. /* Wait for the value to take effect on gpio. */
  498. msleep(100);
  499. if (retval) {
  500. FMDERR("%s:enable irq failed\n", __func__);
  501. goto open_err_req_irq;
  502. }
  503. return retval;
  504. open_err_req_irq:
  505. rtc6226_fm_power_cfg(radio, TURNING_OFF);
  506. open_err_setup:
  507. atomic_dec(&radio->users);
  508. return retval;
  509. }
  510. /*
  511. * rtc6226_fops_release - file release
  512. */
  513. int rtc6226_fops_release(struct file *file)
  514. {
  515. struct rtc6226_device *radio = video_drvdata(file);
  516. int retval = 0;
  517. FMDBG("%s : Exit\n", __func__);
  518. if (radio->mode != FM_OFF) {
  519. rtc6226_power_down(radio);
  520. radio->mode = FM_OFF;
  521. }
  522. rtc6226_disable_irq(radio);
  523. atomic_dec(&radio->users);
  524. retval = rtc6226_fm_power_cfg(radio, TURNING_OFF);
  525. if (retval < 0)
  526. FMDERR("%s: failed to apply voltage\n", __func__);
  527. return retval;
  528. }
  529. static int rtc6226_parse_dt(struct device *dev,
  530. struct rtc6226_device *radio)
  531. {
  532. int rc = 0;
  533. struct device_node *np = dev->of_node;
  534. radio->int_gpio = of_get_named_gpio(np, "fmint-gpio", 0);
  535. if (radio->int_gpio < 0) {
  536. FMDERR("%s int-gpio not provided in device tree\n", __func__);
  537. rc = radio->int_gpio;
  538. goto err_int_gpio;
  539. }
  540. rc = gpio_request(radio->int_gpio, "fm_int");
  541. if (rc) {
  542. FMDERR("%s unable to request gpio %d (%d)\n", __func__,
  543. radio->int_gpio, rc);
  544. goto err_int_gpio;
  545. }
  546. rc = gpio_direction_output(radio->int_gpio, 0);
  547. if (rc) {
  548. FMDERR("%s unable to set the gpio %d direction(%d)\n",
  549. __func__, radio->int_gpio, rc);
  550. goto err_int_gpio;
  551. }
  552. /* Wait for the value to take effect on gpio. */
  553. msleep(100);
  554. return rc;
  555. err_int_gpio:
  556. gpio_free(radio->int_gpio);
  557. return rc;
  558. }
  559. static int rtc6226_pinctrl_init(struct rtc6226_device *radio)
  560. {
  561. int retval = 0;
  562. radio->fm_pinctrl = devm_pinctrl_get(&radio->client->dev);
  563. if (IS_ERR_OR_NULL(radio->fm_pinctrl)) {
  564. FMDERR("%s: target does not use pinctrl\n", __func__);
  565. retval = PTR_ERR(radio->fm_pinctrl);
  566. return retval;
  567. }
  568. radio->gpio_state_active =
  569. pinctrl_lookup_state(radio->fm_pinctrl,
  570. "pmx_fm_active");
  571. if (IS_ERR_OR_NULL(radio->gpio_state_active)) {
  572. FMDERR("%s: cannot get FM active state\n", __func__);
  573. retval = PTR_ERR(radio->gpio_state_active);
  574. goto err_active_state;
  575. }
  576. radio->gpio_state_suspend =
  577. pinctrl_lookup_state(radio->fm_pinctrl,
  578. "pmx_fm_suspend");
  579. if (IS_ERR_OR_NULL(radio->gpio_state_suspend)) {
  580. FMDERR("%s: cannot get FM suspend state\n", __func__);
  581. retval = PTR_ERR(radio->gpio_state_suspend);
  582. goto err_suspend_state;
  583. }
  584. return retval;
  585. err_suspend_state:
  586. radio->gpio_state_suspend = 0;
  587. err_active_state:
  588. radio->gpio_state_active = 0;
  589. return retval;
  590. }
  591. static int rtc6226_dt_parse_vreg_info(struct device *dev,
  592. struct fm_power_vreg_data *vreg, const char *vreg_name)
  593. {
  594. int ret = 0;
  595. u32 vol_suply[2];
  596. struct device_node *np = dev->of_node;
  597. ret = of_property_read_u32_array(np, vreg_name, vol_suply, 2);
  598. if (ret < 0) {
  599. FMDERR("Invalid property name\n");
  600. ret = -EINVAL;
  601. } else {
  602. vreg->low_vol_level = vol_suply[0];
  603. vreg->high_vol_level = vol_suply[1];
  604. }
  605. return ret;
  606. }
  607. /*
  608. * rtc6226_i2c_probe - probe for the device
  609. */
  610. static int rtc6226_i2c_probe(struct i2c_client *client,
  611. const struct i2c_device_id *id)
  612. {
  613. struct rtc6226_device *radio;
  614. struct v4l2_device *v4l2_dev;
  615. struct v4l2_ctrl_handler *hdl;
  616. struct regulator *vddvreg = NULL;
  617. struct regulator *viovreg = NULL;
  618. int retval = 0;
  619. int i = 0;
  620. int kfifo_alloc_rc = 0;
  621. /* struct v4l2_ctrl *ctrl; */
  622. /* need to add description "irq-fm" in dts */
  623. FMDBG("%s enter\n", __func__);
  624. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  625. retval = -ENODEV;
  626. return retval;
  627. }
  628. /*
  629. * if voltage regulator is not ready yet, return the error
  630. * if error is -EPROBE_DEFER to kernel then probe will be called at
  631. * later point of time.
  632. */
  633. viovreg = regulator_get(&client->dev, "vio");
  634. if (IS_ERR(viovreg)) {
  635. retval = PTR_ERR(viovreg);
  636. FMDERR("%s: regulator_get(vio) failed. retval=%d\n",
  637. __func__, retval);
  638. return retval;
  639. }
  640. vddvreg = regulator_get(&client->dev, "vdd");
  641. if (IS_ERR(vddvreg)) {
  642. retval = PTR_ERR(vddvreg);
  643. FMDERR("%s: regulator_get(vdd) failed. retval=%d\n",
  644. __func__, retval);
  645. regulator_put(viovreg);
  646. return retval;
  647. }
  648. /* private data allocation and initialization */
  649. radio = kzalloc(sizeof(struct rtc6226_device), GFP_KERNEL);
  650. if (!radio) {
  651. retval = -ENOMEM;
  652. regulator_put(viovreg);
  653. regulator_put(vddvreg);
  654. return retval;
  655. }
  656. v4l2_dev = &radio->v4l2_dev;
  657. retval = v4l2_device_register(&client->dev, v4l2_dev);
  658. if (retval < 0) {
  659. FMDERR("%s couldn't register v4l2_device\n", __func__);
  660. goto err_vreg;
  661. }
  662. FMDBG("v4l2_device_register successfully\n");
  663. hdl = &radio->ctrl_handler;
  664. /* initialize the device count */
  665. atomic_set(&radio->users, 0);
  666. radio->client = client;
  667. mutex_init(&radio->lock);
  668. init_completion(&radio->completion);
  669. retval = rtc6226_parse_dt(&client->dev, radio);
  670. if (retval) {
  671. FMDERR("%s: Parsing DT failed(%d)\n", __func__, retval);
  672. goto err_v4l2;
  673. }
  674. radio->vddreg = devm_kzalloc(&client->dev,
  675. sizeof(struct fm_power_vreg_data),
  676. GFP_KERNEL);
  677. if (!radio->vddreg) {
  678. FMDERR("%s: allocating memory for vdd vreg failed\n",
  679. __func__);
  680. retval = -ENOMEM;
  681. goto err_v4l2;
  682. }
  683. radio->vddreg->reg = vddvreg;
  684. radio->vddreg->name = "vdd";
  685. radio->vddreg->is_enabled = false;
  686. of_property_read_u32(client->dev.of_node,
  687. "rtc6226,vdd-load", &radio->vddreg->vdd_load);
  688. FMDERR("%s: rtc6226,vdd-load val %d\n",
  689. __func__, radio->vddreg->vdd_load);
  690. retval = rtc6226_dt_parse_vreg_info(&client->dev,
  691. radio->vddreg, "rtc6226,vdd-supply-voltage");
  692. if (retval < 0) {
  693. FMDERR("%s: parsing vdd-supply failed\n", __func__);
  694. goto err_v4l2;
  695. }
  696. radio->vioreg = devm_kzalloc(&client->dev,
  697. sizeof(struct fm_power_vreg_data),
  698. GFP_KERNEL);
  699. if (!radio->vioreg) {
  700. FMDERR("%s: allocating memory for vio vreg failed\n",
  701. __func__);
  702. retval = -ENOMEM;
  703. goto err_v4l2;
  704. }
  705. radio->vioreg->reg = viovreg;
  706. radio->vioreg->name = "vio";
  707. radio->vioreg->is_enabled = false;
  708. retval = rtc6226_dt_parse_vreg_info(&client->dev,
  709. radio->vioreg, "rtc6226,vio-supply-voltage");
  710. if (retval < 0) {
  711. FMDERR("%s: parsing vio-supply failed\n", __func__);
  712. goto err_v4l2;
  713. }
  714. /* Initialize pin control*/
  715. retval = rtc6226_pinctrl_init(radio);
  716. if (retval) {
  717. FMDERR("%s: rtc6226_pinctrl_init returned %d\n",
  718. __func__, retval);
  719. /* if pinctrl is not supported, -EINVAL is returned*/
  720. if (retval == -EINVAL)
  721. retval = 0;
  722. } else {
  723. FMDBG("%s rtc6226_pinctrl_init success\n", __func__);
  724. }
  725. memcpy(&radio->videodev, &rtc6226_viddev_template,
  726. sizeof(struct video_device));
  727. radio->videodev.v4l2_dev = v4l2_dev;
  728. radio->videodev.ioctl_ops = &rtc6226_ioctl_ops;
  729. radio->videodev.device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE
  730. | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  731. video_set_drvdata(&radio->videodev, radio);
  732. /* rds buffer allocation */
  733. radio->buf_size = rds_buf * 3;
  734. radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
  735. if (!radio->buffer) {
  736. retval = -EIO;
  737. goto err;
  738. }
  739. for (i = 0; i < RTC6226_FM_BUF_MAX; i++) {
  740. spin_lock_init(&radio->buf_lock[i]);
  741. kfifo_alloc_rc = kfifo_alloc(&radio->data_buf[i],
  742. STD_BUF_SIZE, GFP_KERNEL);
  743. if (kfifo_alloc_rc != 0) {
  744. FMDERR("%s: failed allocating buffers %d\n",
  745. __func__, kfifo_alloc_rc);
  746. retval = -ENOMEM;
  747. goto err_rds;
  748. }
  749. }
  750. radio->wqueue = NULL;
  751. radio->wqueue_scan = NULL;
  752. radio->wqueue_rds = NULL;
  753. radio->band = -1;
  754. /* rds buffer configuration */
  755. radio->wr_index = 0;
  756. radio->rd_index = 0;
  757. init_waitqueue_head(&radio->event_queue);
  758. init_waitqueue_head(&radio->read_queue);
  759. init_waitqueue_head(&rtc6226_wq);
  760. radio->wqueue = create_singlethread_workqueue("fmradio");
  761. if (!radio->wqueue) {
  762. retval = -ENOMEM;
  763. goto err_rds;
  764. }
  765. radio->wqueue_scan = create_singlethread_workqueue("fmradioscan");
  766. if (!radio->wqueue_scan) {
  767. retval = -ENOMEM;
  768. goto err_wqueue;
  769. }
  770. radio->wqueue_rds = create_singlethread_workqueue("fmradiords");
  771. if (!radio->wqueue_rds) {
  772. retval = -ENOMEM;
  773. goto err_wqueue_scan;
  774. }
  775. /* register video device */
  776. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
  777. radio_nr);
  778. if (retval) {
  779. dev_info(&client->dev, "Could not register video device\n");
  780. goto err_all;
  781. }
  782. i2c_set_clientdata(client, radio); /* move from below */
  783. FMDBG("%s exit\n", __func__);
  784. return 0;
  785. err_all:
  786. destroy_workqueue(radio->wqueue_rds);
  787. err_wqueue_scan:
  788. destroy_workqueue(radio->wqueue_scan);
  789. err_wqueue:
  790. destroy_workqueue(radio->wqueue);
  791. err_rds:
  792. kfree(radio->buffer);
  793. err:
  794. video_device_release_empty(&radio->videodev);
  795. err_v4l2:
  796. v4l2_device_unregister(v4l2_dev);
  797. err_vreg:
  798. if (radio && radio->vioreg && radio->vioreg->reg) {
  799. regulator_put(radio->vioreg->reg);
  800. devm_kfree(&client->dev, radio->vioreg);
  801. } else {
  802. regulator_put(viovreg);
  803. }
  804. if (radio && radio->vddreg && radio->vddreg->reg) {
  805. regulator_put(radio->vddreg->reg);
  806. devm_kfree(&client->dev, radio->vddreg);
  807. } else {
  808. regulator_put(vddvreg);
  809. }
  810. kfree(radio);
  811. return retval;
  812. }
  813. /*
  814. * rtc6226_i2c_remove - remove the device
  815. */
  816. static int rtc6226_i2c_remove(struct i2c_client *client)
  817. {
  818. struct rtc6226_device *radio = i2c_get_clientdata(client);
  819. free_irq(client->irq, radio);
  820. kfree(radio->buffer);
  821. v4l2_ctrl_handler_free(&radio->ctrl_handler);
  822. if (video_is_registered(&radio->videodev))
  823. video_unregister_device(&radio->videodev);
  824. video_device_release_empty(&radio->videodev);
  825. v4l2_device_unregister(&radio->v4l2_dev);
  826. kfree(radio);
  827. FMDBG("%s exit\n", __func__);
  828. return 0;
  829. }
  830. #ifdef CONFIG_PM
  831. /*
  832. * rtc6226_i2c_suspend - suspend the device
  833. */
  834. static int rtc6226_i2c_suspend(struct device *dev)
  835. {
  836. struct i2c_client *client = to_i2c_client(dev);
  837. struct rtc6226_device *radio = i2c_get_clientdata(client);
  838. FMDBG("%s %d\n", __func__, radio->client->addr);
  839. return 0;
  840. }
  841. /*
  842. * rtc6226_i2c_resume - resume the device
  843. */
  844. static int rtc6226_i2c_resume(struct device *dev)
  845. {
  846. struct i2c_client *client = to_i2c_client(dev);
  847. struct rtc6226_device *radio = i2c_get_clientdata(client);
  848. FMDBG("%s %d\n", __func__, radio->client->addr);
  849. return 0;
  850. }
  851. static SIMPLE_DEV_PM_OPS(rtc6226_i2c_pm, rtc6226_i2c_suspend,
  852. rtc6226_i2c_resume);
  853. #endif
  854. /*
  855. * rtc6226_i2c_driver - i2c driver interface
  856. */
  857. struct i2c_driver rtc6226_i2c_driver = {
  858. .driver = {
  859. .name = "rtc6226",
  860. .owner = THIS_MODULE,
  861. .of_match_table = of_match_ptr(rtc6226_i2c_dt_ids),
  862. #ifdef CONFIG_PM
  863. .pm = &rtc6226_i2c_pm,
  864. #endif
  865. },
  866. .probe = rtc6226_i2c_probe,
  867. .remove = rtc6226_i2c_remove,
  868. .id_table = rtc6226_i2c_id,
  869. };
  870. /*
  871. * rtc6226_i2c_init
  872. */
  873. int rtc6226_i2c_init(void)
  874. {
  875. FMDBG(DRIVER_DESC ", Version " DRIVER_VERSION "\n");
  876. return i2c_add_driver(&rtc6226_i2c_driver);
  877. }
  878. MODULE_LICENSE("GPL v2");
  879. MODULE_DESCRIPTION(DRIVER_DESC);
  880. MODULE_VERSION(DRIVER_VERSION);