1 /* 2 * Driver for AK8813 / AK8814 TV-ecoders from Asahi Kasei Microsystems Co., Ltd. (AKM) 3 * 4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/i2c.h> 12 #include <linux/init.h> 13 #include <linux/platform_device.h> 14 #include <linux/slab.h> 15 #include <linux/videodev2.h> 16 #include <linux/module.h> 17 18 #include <media/ak881x.h> 19 #include <media/v4l2-chip-ident.h> 20 #include <media/v4l2-common.h> 21 #include <media/v4l2-device.h> 22 23 #define AK881X_INTERFACE_MODE 0 24 #define AK881X_VIDEO_PROCESS1 1 25 #define AK881X_VIDEO_PROCESS2 2 26 #define AK881X_VIDEO_PROCESS3 3 27 #define AK881X_DAC_MODE 5 28 #define AK881X_STATUS 0x24 29 #define AK881X_DEVICE_ID 0x25 30 #define AK881X_DEVICE_REVISION 0x26 31 32 struct ak881x { 33 struct v4l2_subdev subdev; 34 struct ak881x_pdata *pdata; 35 unsigned int lines; 36 int id; /* DEVICE_ID code V4L2_IDENT_AK881X code from v4l2-chip-ident.h */ 37 char revision; /* DEVICE_REVISION content */ 38 }; 39 40 static int reg_read(struct i2c_client *client, const u8 reg) 41 { 42 return i2c_smbus_read_byte_data(client, reg); 43 } 44 45 static int reg_write(struct i2c_client *client, const u8 reg, 46 const u8 data) 47 { 48 return i2c_smbus_write_byte_data(client, reg, data); 49 } 50 51 static int reg_set(struct i2c_client *client, const u8 reg, 52 const u8 data, u8 mask) 53 { 54 int ret = reg_read(client, reg); 55 if (ret < 0) 56 return ret; 57 return reg_write(client, reg, (ret & ~mask) | (data & mask)); 58 } 59 60 static struct ak881x *to_ak881x(const struct i2c_client *client) 61 { 62 return container_of(i2c_get_clientdata(client), struct ak881x, subdev); 63 } 64 65 static int ak881x_g_chip_ident(struct v4l2_subdev *sd, 66 struct v4l2_dbg_chip_ident *id) 67 { 68 struct i2c_client *client = v4l2_get_subdevdata(sd); 69 struct ak881x *ak881x = to_ak881x(client); 70 71 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) 72 return -EINVAL; 73 74 if (id->match.addr != client->addr) 75 return -ENODEV; 76 77 id->ident = ak881x->id; 78 id->revision = ak881x->revision; 79 80 return 0; 81 } 82 83 #ifdef CONFIG_VIDEO_ADV_DEBUG 84 static int ak881x_g_register(struct v4l2_subdev *sd, 85 struct v4l2_dbg_register *reg) 86 { 87 struct i2c_client *client = v4l2_get_subdevdata(sd); 88 89 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26) 90 return -EINVAL; 91 92 if (reg->match.addr != client->addr) 93 return -ENODEV; 94 95 reg->val = reg_read(client, reg->reg); 96 97 if (reg->val > 0xffff) 98 return -EIO; 99 100 return 0; 101 } 102 103 static int ak881x_s_register(struct v4l2_subdev *sd, 104 const struct v4l2_dbg_register *reg) 105 { 106 struct i2c_client *client = v4l2_get_subdevdata(sd); 107 108 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26) 109 return -EINVAL; 110 111 if (reg->match.addr != client->addr) 112 return -ENODEV; 113 114 if (reg_write(client, reg->reg, reg->val) < 0) 115 return -EIO; 116 117 return 0; 118 } 119 #endif 120 121 static int ak881x_try_g_mbus_fmt(struct v4l2_subdev *sd, 122 struct v4l2_mbus_framefmt *mf) 123 { 124 struct i2c_client *client = v4l2_get_subdevdata(sd); 125 struct ak881x *ak881x = to_ak881x(client); 126 127 v4l_bound_align_image(&mf->width, 0, 720, 2, 128 &mf->height, 0, ak881x->lines, 1, 0); 129 mf->field = V4L2_FIELD_INTERLACED; 130 mf->code = V4L2_MBUS_FMT_YUYV8_2X8; 131 mf->colorspace = V4L2_COLORSPACE_SMPTE170M; 132 133 return 0; 134 } 135 136 static int ak881x_s_mbus_fmt(struct v4l2_subdev *sd, 137 struct v4l2_mbus_framefmt *mf) 138 { 139 if (mf->field != V4L2_FIELD_INTERLACED || 140 mf->code != V4L2_MBUS_FMT_YUYV8_2X8) 141 return -EINVAL; 142 143 return ak881x_try_g_mbus_fmt(sd, mf); 144 } 145 146 static int ak881x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index, 147 enum v4l2_mbus_pixelcode *code) 148 { 149 if (index) 150 return -EINVAL; 151 152 *code = V4L2_MBUS_FMT_YUYV8_2X8; 153 return 0; 154 } 155 156 static int ak881x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a) 157 { 158 struct i2c_client *client = v4l2_get_subdevdata(sd); 159 struct ak881x *ak881x = to_ak881x(client); 160 161 a->bounds.left = 0; 162 a->bounds.top = 0; 163 a->bounds.width = 720; 164 a->bounds.height = ak881x->lines; 165 a->defrect = a->bounds; 166 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 167 a->pixelaspect.numerator = 1; 168 a->pixelaspect.denominator = 1; 169 170 return 0; 171 } 172 173 static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std) 174 { 175 struct i2c_client *client = v4l2_get_subdevdata(sd); 176 struct ak881x *ak881x = to_ak881x(client); 177 u8 vp1; 178 179 if (std == V4L2_STD_NTSC_443) { 180 vp1 = 3; 181 ak881x->lines = 480; 182 } else if (std == V4L2_STD_PAL_M) { 183 vp1 = 5; 184 ak881x->lines = 480; 185 } else if (std == V4L2_STD_PAL_60) { 186 vp1 = 7; 187 ak881x->lines = 480; 188 } else if (std && !(std & ~V4L2_STD_PAL)) { 189 vp1 = 0xf; 190 ak881x->lines = 576; 191 } else if (std && !(std & ~V4L2_STD_NTSC)) { 192 vp1 = 0; 193 ak881x->lines = 480; 194 } else { 195 /* No SECAM or PAL_N/Nc supported */ 196 return -EINVAL; 197 } 198 199 reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf); 200 201 return 0; 202 } 203 204 static int ak881x_s_stream(struct v4l2_subdev *sd, int enable) 205 { 206 struct i2c_client *client = v4l2_get_subdevdata(sd); 207 struct ak881x *ak881x = to_ak881x(client); 208 209 if (enable) { 210 u8 dac; 211 /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */ 212 /* Default: composite output */ 213 if (ak881x->pdata->flags & AK881X_COMPONENT) 214 dac = 3; 215 else 216 dac = 4; 217 /* Turn on the DAC(s) */ 218 reg_write(client, AK881X_DAC_MODE, dac); 219 dev_dbg(&client->dev, "chip status 0x%x\n", 220 reg_read(client, AK881X_STATUS)); 221 } else { 222 /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */ 223 reg_write(client, AK881X_DAC_MODE, 0); 224 dev_dbg(&client->dev, "chip status 0x%x\n", 225 reg_read(client, AK881X_STATUS)); 226 } 227 228 return 0; 229 } 230 231 static struct v4l2_subdev_core_ops ak881x_subdev_core_ops = { 232 .g_chip_ident = ak881x_g_chip_ident, 233 #ifdef CONFIG_VIDEO_ADV_DEBUG 234 .g_register = ak881x_g_register, 235 .s_register = ak881x_s_register, 236 #endif 237 }; 238 239 static struct v4l2_subdev_video_ops ak881x_subdev_video_ops = { 240 .s_mbus_fmt = ak881x_s_mbus_fmt, 241 .g_mbus_fmt = ak881x_try_g_mbus_fmt, 242 .try_mbus_fmt = ak881x_try_g_mbus_fmt, 243 .cropcap = ak881x_cropcap, 244 .enum_mbus_fmt = ak881x_enum_mbus_fmt, 245 .s_std_output = ak881x_s_std_output, 246 .s_stream = ak881x_s_stream, 247 }; 248 249 static struct v4l2_subdev_ops ak881x_subdev_ops = { 250 .core = &ak881x_subdev_core_ops, 251 .video = &ak881x_subdev_video_ops, 252 }; 253 254 static int ak881x_probe(struct i2c_client *client, 255 const struct i2c_device_id *did) 256 { 257 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 258 struct ak881x *ak881x; 259 u8 ifmode, data; 260 261 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 262 dev_warn(&adapter->dev, 263 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); 264 return -EIO; 265 } 266 267 ak881x = kzalloc(sizeof(struct ak881x), GFP_KERNEL); 268 if (!ak881x) 269 return -ENOMEM; 270 271 v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops); 272 273 data = reg_read(client, AK881X_DEVICE_ID); 274 275 switch (data) { 276 case 0x13: 277 ak881x->id = V4L2_IDENT_AK8813; 278 break; 279 case 0x14: 280 ak881x->id = V4L2_IDENT_AK8814; 281 break; 282 default: 283 dev_err(&client->dev, 284 "No ak881x chip detected, register read %x\n", data); 285 kfree(ak881x); 286 return -ENODEV; 287 } 288 289 ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION); 290 ak881x->pdata = client->dev.platform_data; 291 292 if (ak881x->pdata) { 293 if (ak881x->pdata->flags & AK881X_FIELD) 294 ifmode = 4; 295 else 296 ifmode = 0; 297 298 switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) { 299 case AK881X_IF_MODE_BT656: 300 ifmode |= 1; 301 break; 302 case AK881X_IF_MODE_MASTER: 303 ifmode |= 2; 304 break; 305 case AK881X_IF_MODE_SLAVE: 306 default: 307 break; 308 } 309 310 dev_dbg(&client->dev, "IF mode %x\n", ifmode); 311 312 /* 313 * "Line Blanking No." seems to be the same as the number of 314 * "black" lines on, e.g., SuperH VOU, whose default value of 20 315 * "incidentally" matches ak881x' default 316 */ 317 reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3)); 318 } 319 320 /* Hardware default: NTSC-M */ 321 ak881x->lines = 480; 322 323 dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n", 324 data, ak881x->revision); 325 326 return 0; 327 } 328 329 static int ak881x_remove(struct i2c_client *client) 330 { 331 struct ak881x *ak881x = to_ak881x(client); 332 333 v4l2_device_unregister_subdev(&ak881x->subdev); 334 kfree(ak881x); 335 336 return 0; 337 } 338 339 static const struct i2c_device_id ak881x_id[] = { 340 { "ak8813", 0 }, 341 { "ak8814", 0 }, 342 { } 343 }; 344 MODULE_DEVICE_TABLE(i2c, ak881x_id); 345 346 static struct i2c_driver ak881x_i2c_driver = { 347 .driver = { 348 .name = "ak881x", 349 }, 350 .probe = ak881x_probe, 351 .remove = ak881x_remove, 352 .id_table = ak881x_id, 353 }; 354 355 module_i2c_driver(ak881x_i2c_driver); 356 357 MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814"); 358 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); 359 MODULE_LICENSE("GPL v2"); 360