Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 1 | /* |
| 2 | * drivers/media/radio/si470x/radio-si470x-i2c.c |
| 3 | * |
| 4 | * I2C driver for radios with Silicon Labs Si470x FM Radio Receivers |
| 5 | * |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 6 | * Copyright (c) 2009 Samsung Electronics Co.Ltd |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 7 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> |
| 8 | * |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 13 | * |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 18 | * |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 22 | */ |
| 23 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 24 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 25 | /* driver definitions */ |
| 26 | #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>"; |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 27 | #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver" |
| 28 | #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers" |
Mauro Carvalho Chehab | 29834c1 | 2011-06-25 10:15:42 -0300 | [diff] [blame] | 29 | #define DRIVER_VERSION "1.0.2" |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 30 | |
| 31 | /* kernel includes */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 32 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 34 | #include <linux/delay.h> |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 35 | #include <linux/interrupt.h> |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 36 | |
| 37 | #include "radio-si470x.h" |
| 38 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 39 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 40 | /* I2C Device ID List */ |
| 41 | static const struct i2c_device_id si470x_i2c_id[] = { |
| 42 | /* Generic Entry */ |
| 43 | { "si470x", 0 }, |
| 44 | /* Terminating entry */ |
| 45 | { } |
| 46 | }; |
| 47 | MODULE_DEVICE_TABLE(i2c, si470x_i2c_id); |
| 48 | |
| 49 | |
| 50 | |
| 51 | /************************************************************************** |
| 52 | * Module Parameters |
| 53 | **************************************************************************/ |
| 54 | |
| 55 | /* Radio Nr */ |
| 56 | static int radio_nr = -1; |
| 57 | module_param(radio_nr, int, 0444); |
| 58 | MODULE_PARM_DESC(radio_nr, "Radio Nr"); |
| 59 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 60 | /* RDS buffer blocks */ |
| 61 | static unsigned int rds_buf = 100; |
| 62 | module_param(rds_buf, uint, 0444); |
| 63 | MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*"); |
| 64 | |
| 65 | /* RDS maximum block errors */ |
| 66 | static unsigned short max_rds_errors = 1; |
| 67 | /* 0 means 0 errors requiring correction */ |
| 68 | /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */ |
| 69 | /* 2 means 3-5 errors requiring correction */ |
| 70 | /* 3 means 6+ errors or errors in checkword, correction not possible */ |
| 71 | module_param(max_rds_errors, ushort, 0644); |
| 72 | MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*"); |
| 73 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 74 | |
| 75 | |
| 76 | /************************************************************************** |
| 77 | * I2C Definitions |
| 78 | **************************************************************************/ |
| 79 | |
| 80 | /* Write starts with the upper byte of register 0x02 */ |
| 81 | #define WRITE_REG_NUM 8 |
| 82 | #define WRITE_INDEX(i) (i + 0x02) |
| 83 | |
| 84 | /* Read starts with the upper byte of register 0x0a */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 85 | #define READ_REG_NUM RADIO_REGISTER_NUM |
| 86 | #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM) |
| 87 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 88 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 89 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 90 | /************************************************************************** |
| 91 | * General Driver Functions - REGISTERs |
| 92 | **************************************************************************/ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 93 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 94 | /* |
| 95 | * si470x_get_register - read register |
| 96 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 97 | int si470x_get_register(struct si470x_device *radio, int regnr) |
| 98 | { |
| 99 | u16 buf[READ_REG_NUM]; |
| 100 | struct i2c_msg msgs[1] = { |
Shubhrajyoti D | 058fef6 | 2012-09-18 08:22:36 -0300 | [diff] [blame] | 101 | { |
| 102 | .addr = radio->client->addr, |
| 103 | .flags = I2C_M_RD, |
| 104 | .len = sizeof(u16) * READ_REG_NUM, |
| 105 | .buf = (void *)buf |
| 106 | }, |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 110 | return -EIO; |
| 111 | |
| 112 | radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 117 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 118 | /* |
| 119 | * si470x_set_register - write register |
| 120 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 121 | int si470x_set_register(struct si470x_device *radio, int regnr) |
| 122 | { |
| 123 | int i; |
| 124 | u16 buf[WRITE_REG_NUM]; |
| 125 | struct i2c_msg msgs[1] = { |
Shubhrajyoti D | 058fef6 | 2012-09-18 08:22:36 -0300 | [diff] [blame] | 126 | { |
| 127 | .addr = radio->client->addr, |
| 128 | .len = sizeof(u16) * WRITE_REG_NUM, |
| 129 | .buf = (void *)buf |
| 130 | }, |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | for (i = 0; i < WRITE_REG_NUM; i++) |
| 134 | buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]); |
| 135 | |
| 136 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 137 | return -EIO; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 142 | |
| 143 | |
| 144 | /************************************************************************** |
| 145 | * General Driver Functions - ENTIRE REGISTERS |
| 146 | **************************************************************************/ |
| 147 | |
| 148 | /* |
| 149 | * si470x_get_all_registers - read entire registers |
| 150 | */ |
| 151 | static int si470x_get_all_registers(struct si470x_device *radio) |
| 152 | { |
| 153 | int i; |
| 154 | u16 buf[READ_REG_NUM]; |
| 155 | struct i2c_msg msgs[1] = { |
Shubhrajyoti D | 058fef6 | 2012-09-18 08:22:36 -0300 | [diff] [blame] | 156 | { |
| 157 | .addr = radio->client->addr, |
| 158 | .flags = I2C_M_RD, |
| 159 | .len = sizeof(u16) * READ_REG_NUM, |
| 160 | .buf = (void *)buf |
| 161 | }, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 165 | return -EIO; |
| 166 | |
| 167 | for (i = 0; i < READ_REG_NUM; i++) |
| 168 | radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | /************************************************************************** |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 176 | * File Operations Interface |
| 177 | **************************************************************************/ |
| 178 | |
| 179 | /* |
| 180 | * si470x_fops_open - file open |
| 181 | */ |
Joonyoung Shim | 1aa925c | 2009-12-10 16:49:34 -0300 | [diff] [blame] | 182 | int si470x_fops_open(struct file *file) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 183 | { |
| 184 | struct si470x_device *radio = video_drvdata(file); |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 185 | int retval = v4l2_fh_open(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 186 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 187 | if (retval) |
| 188 | return retval; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 189 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 190 | if (v4l2_fh_is_singular_file(file)) { |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 191 | /* start radio */ |
| 192 | retval = si470x_start(radio); |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 193 | if (retval < 0) |
| 194 | goto done; |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 195 | |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 196 | /* enable RDS / STC interrupt */ |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 197 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN; |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 198 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 199 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2; |
| 200 | radio->registers[SYSCONFIG1] |= 0x1 << 2; |
| 201 | retval = si470x_set_register(radio, SYSCONFIG1); |
| 202 | } |
| 203 | |
| 204 | done: |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 205 | if (retval) |
| 206 | v4l2_fh_release(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 207 | return retval; |
| 208 | } |
| 209 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * si470x_fops_release - file release |
| 213 | */ |
Joonyoung Shim | 1aa925c | 2009-12-10 16:49:34 -0300 | [diff] [blame] | 214 | int si470x_fops_release(struct file *file) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 215 | { |
| 216 | struct si470x_device *radio = video_drvdata(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 217 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 218 | if (v4l2_fh_is_singular_file(file)) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 219 | /* stop radio */ |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 220 | si470x_stop(radio); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 221 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 222 | return v4l2_fh_release(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 223 | } |
| 224 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 225 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 226 | |
| 227 | /************************************************************************** |
| 228 | * Video4Linux Interface |
| 229 | **************************************************************************/ |
| 230 | |
| 231 | /* |
| 232 | * si470x_vidioc_querycap - query device capabilities |
| 233 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 234 | int si470x_vidioc_querycap(struct file *file, void *priv, |
| 235 | struct v4l2_capability *capability) |
| 236 | { |
| 237 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); |
| 238 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); |
Hans Verkuil | 3273781 | 2012-08-03 11:16:59 -0300 | [diff] [blame] | 239 | capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE | |
| 240 | V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE; |
| 241 | capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 246 | |
| 247 | |
| 248 | /************************************************************************** |
| 249 | * I2C Interface |
| 250 | **************************************************************************/ |
| 251 | |
| 252 | /* |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 253 | * si470x_i2c_interrupt - interrupt handler |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 254 | */ |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 255 | static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id) |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 256 | { |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 257 | struct si470x_device *radio = dev_id; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 258 | unsigned char regnr; |
| 259 | unsigned char blocknum; |
| 260 | unsigned short bler; /* rds block errors */ |
| 261 | unsigned short rds; |
| 262 | unsigned char tmpbuf[3]; |
| 263 | int retval = 0; |
| 264 | |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 265 | /* check Seek/Tune Complete */ |
| 266 | retval = si470x_get_register(radio, STATUSRSSI); |
| 267 | if (retval < 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 268 | goto end; |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 269 | |
| 270 | if (radio->registers[STATUSRSSI] & STATUSRSSI_STC) |
| 271 | complete(&radio->completion); |
| 272 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 273 | /* safety checks */ |
| 274 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 275 | goto end; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 276 | |
| 277 | /* Update RDS registers */ |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 278 | for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) { |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 279 | retval = si470x_get_register(radio, STATUSRSSI + regnr); |
| 280 | if (retval < 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 281 | goto end; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* get rds blocks */ |
| 285 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) |
| 286 | /* No RDS group ready, better luck next time */ |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 287 | goto end; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 288 | |
| 289 | for (blocknum = 0; blocknum < 4; blocknum++) { |
| 290 | switch (blocknum) { |
| 291 | default: |
| 292 | bler = (radio->registers[STATUSRSSI] & |
| 293 | STATUSRSSI_BLERA) >> 9; |
| 294 | rds = radio->registers[RDSA]; |
| 295 | break; |
| 296 | case 1: |
| 297 | bler = (radio->registers[READCHAN] & |
| 298 | READCHAN_BLERB) >> 14; |
| 299 | rds = radio->registers[RDSB]; |
| 300 | break; |
| 301 | case 2: |
| 302 | bler = (radio->registers[READCHAN] & |
| 303 | READCHAN_BLERC) >> 12; |
| 304 | rds = radio->registers[RDSC]; |
| 305 | break; |
| 306 | case 3: |
| 307 | bler = (radio->registers[READCHAN] & |
| 308 | READCHAN_BLERD) >> 10; |
| 309 | rds = radio->registers[RDSD]; |
| 310 | break; |
Peter Senna Tschudin | c2c1b41 | 2012-09-28 05:37:22 -0300 | [diff] [blame] | 311 | } |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 312 | |
| 313 | /* Fill the V4L2 RDS buffer */ |
| 314 | put_unaligned_le16(rds, &tmpbuf); |
| 315 | tmpbuf[2] = blocknum; /* offset name */ |
| 316 | tmpbuf[2] |= blocknum << 3; /* received offset */ |
| 317 | if (bler > max_rds_errors) |
| 318 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ |
| 319 | else if (bler > 0) |
| 320 | tmpbuf[2] |= 0x40; /* corrected error(s) */ |
| 321 | |
| 322 | /* copy RDS block to internal buffer */ |
| 323 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); |
| 324 | radio->wr_index += 3; |
| 325 | |
| 326 | /* wrap write pointer */ |
| 327 | if (radio->wr_index >= radio->buf_size) |
| 328 | radio->wr_index = 0; |
| 329 | |
| 330 | /* check for overflow */ |
| 331 | if (radio->wr_index == radio->rd_index) { |
| 332 | /* increment and wrap read pointer */ |
| 333 | radio->rd_index += 3; |
| 334 | if (radio->rd_index >= radio->buf_size) |
| 335 | radio->rd_index = 0; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (radio->wr_index != radio->rd_index) |
| 340 | wake_up_interruptible(&radio->read_queue); |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 341 | |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 342 | end: |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 343 | return IRQ_HANDLED; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 348 | * si470x_i2c_probe - probe for the device |
| 349 | */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 350 | static int si470x_i2c_probe(struct i2c_client *client, |
| 351 | const struct i2c_device_id *id) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 352 | { |
| 353 | struct si470x_device *radio; |
| 354 | int retval = 0; |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 355 | unsigned char version_warning = 0; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 356 | |
| 357 | /* private data allocation and initialization */ |
| 358 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); |
| 359 | if (!radio) { |
| 360 | retval = -ENOMEM; |
| 361 | goto err_initial; |
| 362 | } |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 363 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 364 | radio->client = client; |
Hans de Goede | f140612 | 2012-07-12 16:55:48 -0300 | [diff] [blame] | 365 | radio->band = 1; /* Default to 76 - 108 MHz */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 366 | mutex_init(&radio->lock); |
Hans de Goede | 7794711 | 2012-06-14 09:43:12 -0300 | [diff] [blame] | 367 | init_completion(&radio->completion); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 368 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 369 | /* video device initialization */ |
| 370 | radio->videodev = si470x_viddev_template; |
| 371 | video_set_drvdata(&radio->videodev, radio); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 372 | |
| 373 | /* power up : need 110ms */ |
| 374 | radio->registers[POWERCFG] = POWERCFG_ENABLE; |
| 375 | if (si470x_set_register(radio, POWERCFG) < 0) { |
| 376 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 377 | goto err_radio; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 378 | } |
| 379 | msleep(110); |
| 380 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 381 | /* get device and chip versions */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 382 | if (si470x_get_all_registers(radio) < 0) { |
| 383 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 384 | goto err_radio; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 385 | } |
| 386 | dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", |
| 387 | radio->registers[DEVICEID], radio->registers[CHIPID]); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 388 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) { |
| 389 | dev_warn(&client->dev, |
| 390 | "This driver is known to work with " |
| 391 | "firmware version %hu,\n", RADIO_FW_VERSION); |
| 392 | dev_warn(&client->dev, |
| 393 | "but the device has firmware version %hu.\n", |
| 394 | radio->registers[CHIPID] & CHIPID_FIRMWARE); |
| 395 | version_warning = 1; |
| 396 | } |
| 397 | |
| 398 | /* give out version warning */ |
| 399 | if (version_warning == 1) { |
| 400 | dev_warn(&client->dev, |
| 401 | "If you have some trouble using this driver,\n"); |
| 402 | dev_warn(&client->dev, |
| 403 | "please report to V4L ML at " |
| 404 | "linux-media@vger.kernel.org\n"); |
| 405 | } |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 406 | |
| 407 | /* set initial frequency */ |
| 408 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ |
| 409 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 410 | /* rds buffer allocation */ |
| 411 | radio->buf_size = rds_buf * 3; |
| 412 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); |
| 413 | if (!radio->buffer) { |
| 414 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 415 | goto err_radio; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* rds buffer configuration */ |
| 419 | radio->wr_index = 0; |
| 420 | radio->rd_index = 0; |
| 421 | init_waitqueue_head(&radio->read_queue); |
| 422 | |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 423 | retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt, |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 424 | IRQF_TRIGGER_FALLING, DRIVER_NAME, radio); |
| 425 | if (retval) { |
| 426 | dev_err(&client->dev, "Failed to register interrupt\n"); |
| 427 | goto err_rds; |
| 428 | } |
| 429 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 430 | /* register video device */ |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 431 | retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 432 | radio_nr); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 433 | if (retval) { |
| 434 | dev_warn(&client->dev, "Could not register video device\n"); |
| 435 | goto err_all; |
| 436 | } |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 437 | i2c_set_clientdata(client, radio); |
| 438 | |
| 439 | return 0; |
| 440 | err_all: |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 441 | free_irq(client->irq, radio); |
| 442 | err_rds: |
| 443 | kfree(radio->buffer); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 444 | err_radio: |
| 445 | kfree(radio); |
| 446 | err_initial: |
| 447 | return retval; |
| 448 | } |
| 449 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * si470x_i2c_remove - remove the device |
| 453 | */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 454 | static int si470x_i2c_remove(struct i2c_client *client) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 455 | { |
| 456 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 457 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 458 | free_irq(client->irq, radio); |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 459 | video_unregister_device(&radio->videodev); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 460 | kfree(radio); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 465 | |
Mauro Carvalho Chehab | c6c3795 | 2013-11-01 14:24:21 -0300 | [diff] [blame] | 466 | #ifdef CONFIG_PM_SLEEP |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 467 | /* |
| 468 | * si470x_i2c_suspend - suspend the device |
| 469 | */ |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 470 | static int si470x_i2c_suspend(struct device *dev) |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 471 | { |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 472 | struct i2c_client *client = to_i2c_client(dev); |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 473 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 474 | |
| 475 | /* power down */ |
| 476 | radio->registers[POWERCFG] |= POWERCFG_DISABLE; |
| 477 | if (si470x_set_register(radio, POWERCFG) < 0) |
| 478 | return -EIO; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /* |
| 485 | * si470x_i2c_resume - resume the device |
| 486 | */ |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 487 | static int si470x_i2c_resume(struct device *dev) |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 488 | { |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 489 | struct i2c_client *client = to_i2c_client(dev); |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 490 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 491 | |
| 492 | /* power up : need 110ms */ |
| 493 | radio->registers[POWERCFG] |= POWERCFG_ENABLE; |
| 494 | if (si470x_set_register(radio, POWERCFG) < 0) |
| 495 | return -EIO; |
| 496 | msleep(110); |
| 497 | |
| 498 | return 0; |
| 499 | } |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 500 | |
| 501 | static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume); |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 502 | #endif |
| 503 | |
| 504 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 505 | /* |
| 506 | * si470x_i2c_driver - i2c driver interface |
| 507 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 508 | static struct i2c_driver si470x_i2c_driver = { |
| 509 | .driver = { |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 510 | .name = "si470x", |
| 511 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | c6c3795 | 2013-11-01 14:24:21 -0300 | [diff] [blame] | 512 | #ifdef CONFIG_PM_SLEEP |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 513 | .pm = &si470x_i2c_pm, |
| 514 | #endif |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 515 | }, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 516 | .probe = si470x_i2c_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 517 | .remove = si470x_i2c_remove, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 518 | .id_table = si470x_i2c_id, |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 519 | }; |
| 520 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 521 | module_i2c_driver(si470x_i2c_driver); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 522 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 523 | MODULE_LICENSE("GPL"); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 524 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 525 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 526 | MODULE_VERSION(DRIVER_VERSION); |