radio-rtc6226-i2c.c 25 KB

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