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] = { |
| 101 | { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM, |
| 102 | (void *)buf }, |
| 103 | }; |
| 104 | |
| 105 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 106 | return -EIO; |
| 107 | |
| 108 | radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 113 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 114 | /* |
| 115 | * si470x_set_register - write register |
| 116 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 117 | int si470x_set_register(struct si470x_device *radio, int regnr) |
| 118 | { |
| 119 | int i; |
| 120 | u16 buf[WRITE_REG_NUM]; |
| 121 | struct i2c_msg msgs[1] = { |
| 122 | { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM, |
| 123 | (void *)buf }, |
| 124 | }; |
| 125 | |
| 126 | for (i = 0; i < WRITE_REG_NUM; i++) |
| 127 | buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]); |
| 128 | |
| 129 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 130 | return -EIO; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 135 | |
| 136 | |
| 137 | /************************************************************************** |
| 138 | * General Driver Functions - ENTIRE REGISTERS |
| 139 | **************************************************************************/ |
| 140 | |
| 141 | /* |
| 142 | * si470x_get_all_registers - read entire registers |
| 143 | */ |
| 144 | static int si470x_get_all_registers(struct si470x_device *radio) |
| 145 | { |
| 146 | int i; |
| 147 | u16 buf[READ_REG_NUM]; |
| 148 | struct i2c_msg msgs[1] = { |
| 149 | { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM, |
| 150 | (void *)buf }, |
| 151 | }; |
| 152 | |
| 153 | if (i2c_transfer(radio->client->adapter, msgs, 1) != 1) |
| 154 | return -EIO; |
| 155 | |
| 156 | for (i = 0; i < READ_REG_NUM; i++) |
| 157 | radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | /************************************************************************** |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 165 | * File Operations Interface |
| 166 | **************************************************************************/ |
| 167 | |
| 168 | /* |
| 169 | * si470x_fops_open - file open |
| 170 | */ |
Joonyoung Shim | 1aa925c | 2009-12-10 16:49:34 -0300 | [diff] [blame] | 171 | int si470x_fops_open(struct file *file) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 172 | { |
| 173 | struct si470x_device *radio = video_drvdata(file); |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 174 | int retval = v4l2_fh_open(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 175 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 176 | if (retval) |
| 177 | return retval; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 178 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 179 | if (v4l2_fh_is_singular_file(file)) { |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 180 | /* start radio */ |
| 181 | retval = si470x_start(radio); |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 182 | if (retval < 0) |
| 183 | goto done; |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 184 | |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 185 | /* enable RDS / STC interrupt */ |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 186 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN; |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 187 | radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 188 | radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2; |
| 189 | radio->registers[SYSCONFIG1] |= 0x1 << 2; |
| 190 | retval = si470x_set_register(radio, SYSCONFIG1); |
| 191 | } |
| 192 | |
| 193 | done: |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 194 | if (retval) |
| 195 | v4l2_fh_release(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 196 | return retval; |
| 197 | } |
| 198 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * si470x_fops_release - file release |
| 202 | */ |
Joonyoung Shim | 1aa925c | 2009-12-10 16:49:34 -0300 | [diff] [blame] | 203 | int si470x_fops_release(struct file *file) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 204 | { |
| 205 | struct si470x_device *radio = video_drvdata(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 206 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 207 | if (v4l2_fh_is_singular_file(file)) |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 208 | /* stop radio */ |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 209 | si470x_stop(radio); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 210 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 211 | return v4l2_fh_release(file); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 212 | } |
| 213 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 214 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 215 | |
| 216 | /************************************************************************** |
| 217 | * Video4Linux Interface |
| 218 | **************************************************************************/ |
| 219 | |
| 220 | /* |
| 221 | * si470x_vidioc_querycap - query device capabilities |
| 222 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 223 | int si470x_vidioc_querycap(struct file *file, void *priv, |
| 224 | struct v4l2_capability *capability) |
| 225 | { |
| 226 | strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver)); |
| 227 | strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card)); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 228 | capability->capabilities = V4L2_CAP_HW_FREQ_SEEK | |
| 229 | V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 234 | |
| 235 | |
| 236 | /************************************************************************** |
| 237 | * I2C Interface |
| 238 | **************************************************************************/ |
| 239 | |
| 240 | /* |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 241 | * si470x_i2c_interrupt - interrupt handler |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 242 | */ |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 243 | static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id) |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 244 | { |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 245 | struct si470x_device *radio = dev_id; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 246 | unsigned char regnr; |
| 247 | unsigned char blocknum; |
| 248 | unsigned short bler; /* rds block errors */ |
| 249 | unsigned short rds; |
| 250 | unsigned char tmpbuf[3]; |
| 251 | int retval = 0; |
| 252 | |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 253 | /* check Seek/Tune Complete */ |
| 254 | retval = si470x_get_register(radio, STATUSRSSI); |
| 255 | if (retval < 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 256 | goto end; |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 257 | |
| 258 | if (radio->registers[STATUSRSSI] & STATUSRSSI_STC) |
| 259 | complete(&radio->completion); |
| 260 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 261 | /* safety checks */ |
| 262 | if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 263 | goto end; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 264 | |
| 265 | /* Update RDS registers */ |
Joonyoung Shim | 0830be3 | 2011-03-11 03:54:46 -0300 | [diff] [blame] | 266 | for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) { |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 267 | retval = si470x_get_register(radio, STATUSRSSI + regnr); |
| 268 | if (retval < 0) |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 269 | goto end; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* get rds blocks */ |
| 273 | if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0) |
| 274 | /* No RDS group ready, better luck next time */ |
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 | for (blocknum = 0; blocknum < 4; blocknum++) { |
| 278 | switch (blocknum) { |
| 279 | default: |
| 280 | bler = (radio->registers[STATUSRSSI] & |
| 281 | STATUSRSSI_BLERA) >> 9; |
| 282 | rds = radio->registers[RDSA]; |
| 283 | break; |
| 284 | case 1: |
| 285 | bler = (radio->registers[READCHAN] & |
| 286 | READCHAN_BLERB) >> 14; |
| 287 | rds = radio->registers[RDSB]; |
| 288 | break; |
| 289 | case 2: |
| 290 | bler = (radio->registers[READCHAN] & |
| 291 | READCHAN_BLERC) >> 12; |
| 292 | rds = radio->registers[RDSC]; |
| 293 | break; |
| 294 | case 3: |
| 295 | bler = (radio->registers[READCHAN] & |
| 296 | READCHAN_BLERD) >> 10; |
| 297 | rds = radio->registers[RDSD]; |
| 298 | break; |
| 299 | }; |
| 300 | |
| 301 | /* Fill the V4L2 RDS buffer */ |
| 302 | put_unaligned_le16(rds, &tmpbuf); |
| 303 | tmpbuf[2] = blocknum; /* offset name */ |
| 304 | tmpbuf[2] |= blocknum << 3; /* received offset */ |
| 305 | if (bler > max_rds_errors) |
| 306 | tmpbuf[2] |= 0x80; /* uncorrectable errors */ |
| 307 | else if (bler > 0) |
| 308 | tmpbuf[2] |= 0x40; /* corrected error(s) */ |
| 309 | |
| 310 | /* copy RDS block to internal buffer */ |
| 311 | memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3); |
| 312 | radio->wr_index += 3; |
| 313 | |
| 314 | /* wrap write pointer */ |
| 315 | if (radio->wr_index >= radio->buf_size) |
| 316 | radio->wr_index = 0; |
| 317 | |
| 318 | /* check for overflow */ |
| 319 | if (radio->wr_index == radio->rd_index) { |
| 320 | /* increment and wrap read pointer */ |
| 321 | radio->rd_index += 3; |
| 322 | if (radio->rd_index >= radio->buf_size) |
| 323 | radio->rd_index = 0; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (radio->wr_index != radio->rd_index) |
| 328 | wake_up_interruptible(&radio->read_queue); |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 329 | |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 330 | end: |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 331 | return IRQ_HANDLED; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 336 | * si470x_i2c_probe - probe for the device |
| 337 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 338 | static int __devinit si470x_i2c_probe(struct i2c_client *client, |
| 339 | const struct i2c_device_id *id) |
| 340 | { |
| 341 | struct si470x_device *radio; |
| 342 | int retval = 0; |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 343 | unsigned char version_warning = 0; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 344 | |
| 345 | /* private data allocation and initialization */ |
| 346 | radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL); |
| 347 | if (!radio) { |
| 348 | retval = -ENOMEM; |
| 349 | goto err_initial; |
| 350 | } |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 351 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 352 | radio->client = client; |
Hans de Goede | f140612 | 2012-07-12 16:55:48 -0300 | [diff] [blame] | 353 | radio->band = 1; /* Default to 76 - 108 MHz */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 354 | mutex_init(&radio->lock); |
Hans de Goede | 7794711 | 2012-06-14 09:43:12 -0300 | [diff] [blame] | 355 | init_completion(&radio->completion); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 356 | |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 357 | /* video device initialization */ |
| 358 | radio->videodev = si470x_viddev_template; |
| 359 | video_set_drvdata(&radio->videodev, radio); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 360 | |
| 361 | /* power up : need 110ms */ |
| 362 | radio->registers[POWERCFG] = POWERCFG_ENABLE; |
| 363 | if (si470x_set_register(radio, POWERCFG) < 0) { |
| 364 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 365 | goto err_radio; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 366 | } |
| 367 | msleep(110); |
| 368 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 369 | /* get device and chip versions */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 370 | if (si470x_get_all_registers(radio) < 0) { |
| 371 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 372 | goto err_radio; |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 373 | } |
| 374 | dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n", |
| 375 | radio->registers[DEVICEID], radio->registers[CHIPID]); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 376 | if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) { |
| 377 | dev_warn(&client->dev, |
| 378 | "This driver is known to work with " |
| 379 | "firmware version %hu,\n", RADIO_FW_VERSION); |
| 380 | dev_warn(&client->dev, |
| 381 | "but the device has firmware version %hu.\n", |
| 382 | radio->registers[CHIPID] & CHIPID_FIRMWARE); |
| 383 | version_warning = 1; |
| 384 | } |
| 385 | |
| 386 | /* give out version warning */ |
| 387 | if (version_warning == 1) { |
| 388 | dev_warn(&client->dev, |
| 389 | "If you have some trouble using this driver,\n"); |
| 390 | dev_warn(&client->dev, |
| 391 | "please report to V4L ML at " |
| 392 | "linux-media@vger.kernel.org\n"); |
| 393 | } |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 394 | |
| 395 | /* set initial frequency */ |
| 396 | si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */ |
| 397 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 398 | /* rds buffer allocation */ |
| 399 | radio->buf_size = rds_buf * 3; |
| 400 | radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL); |
| 401 | if (!radio->buffer) { |
| 402 | retval = -EIO; |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 403 | goto err_radio; |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* rds buffer configuration */ |
| 407 | radio->wr_index = 0; |
| 408 | radio->rd_index = 0; |
| 409 | init_waitqueue_head(&radio->read_queue); |
| 410 | |
Joonyoung Shim | 474fdc0 | 2011-03-11 03:54:48 -0300 | [diff] [blame] | 411 | retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt, |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 412 | IRQF_TRIGGER_FALLING, DRIVER_NAME, radio); |
| 413 | if (retval) { |
| 414 | dev_err(&client->dev, "Failed to register interrupt\n"); |
| 415 | goto err_rds; |
| 416 | } |
| 417 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 418 | /* register video device */ |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 419 | retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 420 | radio_nr); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 421 | if (retval) { |
| 422 | dev_warn(&client->dev, "Could not register video device\n"); |
| 423 | goto err_all; |
| 424 | } |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 425 | i2c_set_clientdata(client, radio); |
| 426 | |
| 427 | return 0; |
| 428 | err_all: |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 429 | free_irq(client->irq, radio); |
| 430 | err_rds: |
| 431 | kfree(radio->buffer); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 432 | err_radio: |
| 433 | kfree(radio); |
| 434 | err_initial: |
| 435 | return retval; |
| 436 | } |
| 437 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 438 | |
| 439 | /* |
| 440 | * si470x_i2c_remove - remove the device |
| 441 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 442 | static __devexit int si470x_i2c_remove(struct i2c_client *client) |
| 443 | { |
| 444 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 445 | |
Joonyoung Shim | fe2137d | 2009-12-10 16:50:34 -0300 | [diff] [blame] | 446 | free_irq(client->irq, radio); |
Hans Verkuil | 4967d53 | 2012-05-04 09:16:57 -0300 | [diff] [blame] | 447 | video_unregister_device(&radio->videodev); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 448 | kfree(radio); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 453 | |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 454 | #ifdef CONFIG_PM |
| 455 | /* |
| 456 | * si470x_i2c_suspend - suspend the device |
| 457 | */ |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 458 | static int si470x_i2c_suspend(struct device *dev) |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 459 | { |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 460 | struct i2c_client *client = to_i2c_client(dev); |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 461 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 462 | |
| 463 | /* power down */ |
| 464 | radio->registers[POWERCFG] |= POWERCFG_DISABLE; |
| 465 | if (si470x_set_register(radio, POWERCFG) < 0) |
| 466 | return -EIO; |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * si470x_i2c_resume - resume the device |
| 474 | */ |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 475 | static int si470x_i2c_resume(struct device *dev) |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 476 | { |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 477 | struct i2c_client *client = to_i2c_client(dev); |
Joonyoung Shim | d1471f0 | 2009-12-10 16:52:50 -0300 | [diff] [blame] | 478 | struct si470x_device *radio = i2c_get_clientdata(client); |
| 479 | |
| 480 | /* power up : need 110ms */ |
| 481 | radio->registers[POWERCFG] |= POWERCFG_ENABLE; |
| 482 | if (si470x_set_register(radio, POWERCFG) < 0) |
| 483 | return -EIO; |
| 484 | msleep(110); |
| 485 | |
| 486 | return 0; |
| 487 | } |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 488 | |
| 489 | 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] | 490 | #endif |
| 491 | |
| 492 | |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 493 | /* |
| 494 | * si470x_i2c_driver - i2c driver interface |
| 495 | */ |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 496 | static struct i2c_driver si470x_i2c_driver = { |
| 497 | .driver = { |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 498 | .name = "si470x", |
| 499 | .owner = THIS_MODULE, |
Joonyoung Shim | 949cf31 | 2011-03-11 03:54:47 -0300 | [diff] [blame] | 500 | #ifdef CONFIG_PM |
| 501 | .pm = &si470x_i2c_pm, |
| 502 | #endif |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 503 | }, |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 504 | .probe = si470x_i2c_probe, |
| 505 | .remove = __devexit_p(si470x_i2c_remove), |
| 506 | .id_table = si470x_i2c_id, |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 507 | }; |
| 508 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 509 | module_i2c_driver(si470x_i2c_driver); |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 510 | |
Joonyoung Shim | cc35bbd | 2009-08-09 14:23:35 -0300 | [diff] [blame] | 511 | MODULE_LICENSE("GPL"); |
Tobias Lorenz | 9dcb79c | 2009-08-10 18:44:14 -0300 | [diff] [blame] | 512 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 513 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 514 | MODULE_VERSION(DRIVER_VERSION); |