Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 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> |
Randy Dunlap | 1db2c22 | 2010-05-20 18:08:23 -0300 | [diff] [blame] | 14 | #include <linux/slab.h> |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 15 | #include <linux/videodev2.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 17 | |
Mauro Carvalho Chehab | b5dcee2 | 2015-11-10 12:01:44 -0200 | [diff] [blame] | 18 | #include <media/i2c/ak881x.h> |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 19 | #include <media/v4l2-common.h> |
| 20 | #include <media/v4l2-device.h> |
| 21 | |
| 22 | #define AK881X_INTERFACE_MODE 0 |
| 23 | #define AK881X_VIDEO_PROCESS1 1 |
| 24 | #define AK881X_VIDEO_PROCESS2 2 |
| 25 | #define AK881X_VIDEO_PROCESS3 3 |
| 26 | #define AK881X_DAC_MODE 5 |
| 27 | #define AK881X_STATUS 0x24 |
| 28 | #define AK881X_DEVICE_ID 0x25 |
| 29 | #define AK881X_DEVICE_REVISION 0x26 |
| 30 | |
| 31 | struct ak881x { |
| 32 | struct v4l2_subdev subdev; |
| 33 | struct ak881x_pdata *pdata; |
| 34 | unsigned int lines; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 35 | char revision; /* DEVICE_REVISION content */ |
| 36 | }; |
| 37 | |
| 38 | static int reg_read(struct i2c_client *client, const u8 reg) |
| 39 | { |
| 40 | return i2c_smbus_read_byte_data(client, reg); |
| 41 | } |
| 42 | |
| 43 | static int reg_write(struct i2c_client *client, const u8 reg, |
| 44 | const u8 data) |
| 45 | { |
| 46 | return i2c_smbus_write_byte_data(client, reg, data); |
| 47 | } |
| 48 | |
| 49 | static int reg_set(struct i2c_client *client, const u8 reg, |
| 50 | const u8 data, u8 mask) |
| 51 | { |
| 52 | int ret = reg_read(client, reg); |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | return reg_write(client, reg, (ret & ~mask) | (data & mask)); |
| 56 | } |
| 57 | |
| 58 | static struct ak881x *to_ak881x(const struct i2c_client *client) |
| 59 | { |
| 60 | return container_of(i2c_get_clientdata(client), struct ak881x, subdev); |
| 61 | } |
| 62 | |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 63 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 64 | static int ak881x_g_register(struct v4l2_subdev *sd, |
| 65 | struct v4l2_dbg_register *reg) |
| 66 | { |
| 67 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 68 | |
Hans Verkuil | e127711 | 2013-05-29 06:59:51 -0300 | [diff] [blame] | 69 | if (reg->reg > 0x26) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 70 | return -EINVAL; |
| 71 | |
Hans Verkuil | 15c4fee | 2013-05-29 07:00:07 -0300 | [diff] [blame] | 72 | reg->size = 1; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 73 | reg->val = reg_read(client, reg->reg); |
| 74 | |
| 75 | if (reg->val > 0xffff) |
| 76 | return -EIO; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int ak881x_s_register(struct v4l2_subdev *sd, |
Hans Verkuil | 977ba3b | 2013-03-24 08:28:46 -0300 | [diff] [blame] | 82 | const struct v4l2_dbg_register *reg) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 83 | { |
| 84 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 85 | |
Hans Verkuil | e127711 | 2013-05-29 06:59:51 -0300 | [diff] [blame] | 86 | if (reg->reg > 0x26) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 89 | if (reg_write(client, reg->reg, reg->val) < 0) |
| 90 | return -EIO; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | #endif |
| 95 | |
Hans Verkuil | da298c6 | 2015-04-09 04:02:34 -0300 | [diff] [blame] | 96 | static int ak881x_fill_fmt(struct v4l2_subdev *sd, |
| 97 | struct v4l2_subdev_pad_config *cfg, |
| 98 | struct v4l2_subdev_format *format) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 99 | { |
Hans Verkuil | da298c6 | 2015-04-09 04:02:34 -0300 | [diff] [blame] | 100 | struct v4l2_mbus_framefmt *mf = &format->format; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 101 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 102 | struct ak881x *ak881x = to_ak881x(client); |
| 103 | |
Hans Verkuil | da298c6 | 2015-04-09 04:02:34 -0300 | [diff] [blame] | 104 | if (format->pad) |
| 105 | return -EINVAL; |
| 106 | |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 107 | v4l_bound_align_image(&mf->width, 0, 720, 2, |
| 108 | &mf->height, 0, ak881x->lines, 1, 0); |
| 109 | mf->field = V4L2_FIELD_INTERLACED; |
Boris BREZILLON | f5fe58f | 2014-11-10 14:28:29 -0300 | [diff] [blame] | 110 | mf->code = MEDIA_BUS_FMT_YUYV8_2X8; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 111 | mf->colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 116 | static int ak881x_enum_mbus_code(struct v4l2_subdev *sd, |
| 117 | struct v4l2_subdev_pad_config *cfg, |
| 118 | struct v4l2_subdev_mbus_code_enum *code) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 119 | { |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 120 | if (code->pad || code->index) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 121 | return -EINVAL; |
| 122 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 123 | code->code = MEDIA_BUS_FMT_YUYV8_2X8; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 127 | static int ak881x_get_selection(struct v4l2_subdev *sd, |
| 128 | struct v4l2_subdev_pad_config *cfg, |
| 129 | struct v4l2_subdev_selection *sel) |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 130 | { |
| 131 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 132 | struct ak881x *ak881x = to_ak881x(client); |
| 133 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 134 | if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) |
| 135 | return -EINVAL; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 136 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 137 | switch (sel->target) { |
| 138 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 139 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 140 | sel->r.left = 0; |
| 141 | sel->r.top = 0; |
| 142 | sel->r.width = 720; |
| 143 | sel->r.height = ak881x->lines; |
| 144 | return 0; |
| 145 | default: |
| 146 | return -EINVAL; |
| 147 | } |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std) |
| 151 | { |
| 152 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 153 | struct ak881x *ak881x = to_ak881x(client); |
| 154 | u8 vp1; |
| 155 | |
| 156 | if (std == V4L2_STD_NTSC_443) { |
| 157 | vp1 = 3; |
| 158 | ak881x->lines = 480; |
| 159 | } else if (std == V4L2_STD_PAL_M) { |
| 160 | vp1 = 5; |
| 161 | ak881x->lines = 480; |
| 162 | } else if (std == V4L2_STD_PAL_60) { |
| 163 | vp1 = 7; |
| 164 | ak881x->lines = 480; |
Hans Verkuil | 517ef25 | 2015-06-15 08:33:32 -0300 | [diff] [blame] | 165 | } else if (std & V4L2_STD_NTSC) { |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 166 | vp1 = 0; |
| 167 | ak881x->lines = 480; |
Hans Verkuil | 517ef25 | 2015-06-15 08:33:32 -0300 | [diff] [blame] | 168 | } else if (std & V4L2_STD_PAL) { |
| 169 | vp1 = 0xf; |
| 170 | ak881x->lines = 576; |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 171 | } else { |
| 172 | /* No SECAM or PAL_N/Nc supported */ |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int ak881x_s_stream(struct v4l2_subdev *sd, int enable) |
| 182 | { |
| 183 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 184 | struct ak881x *ak881x = to_ak881x(client); |
| 185 | |
| 186 | if (enable) { |
| 187 | u8 dac; |
| 188 | /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */ |
| 189 | /* Default: composite output */ |
| 190 | if (ak881x->pdata->flags & AK881X_COMPONENT) |
| 191 | dac = 3; |
| 192 | else |
| 193 | dac = 4; |
| 194 | /* Turn on the DAC(s) */ |
| 195 | reg_write(client, AK881X_DAC_MODE, dac); |
| 196 | dev_dbg(&client->dev, "chip status 0x%x\n", |
| 197 | reg_read(client, AK881X_STATUS)); |
| 198 | } else { |
| 199 | /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */ |
| 200 | reg_write(client, AK881X_DAC_MODE, 0); |
| 201 | dev_dbg(&client->dev, "chip status 0x%x\n", |
| 202 | reg_read(client, AK881X_STATUS)); |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Bhumika Goyal | 4d7aa5e | 2016-12-13 15:26:13 -0200 | [diff] [blame] | 208 | static const struct v4l2_subdev_core_ops ak881x_subdev_core_ops = { |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 209 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 210 | .g_register = ak881x_g_register, |
| 211 | .s_register = ak881x_s_register, |
| 212 | #endif |
| 213 | }; |
| 214 | |
Bhumika Goyal | 4d7aa5e | 2016-12-13 15:26:13 -0200 | [diff] [blame] | 215 | static const struct v4l2_subdev_video_ops ak881x_subdev_video_ops = { |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 216 | .s_std_output = ak881x_s_std_output, |
| 217 | .s_stream = ak881x_s_stream, |
| 218 | }; |
| 219 | |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 220 | static const struct v4l2_subdev_pad_ops ak881x_subdev_pad_ops = { |
| 221 | .enum_mbus_code = ak881x_enum_mbus_code, |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 222 | .get_selection = ak881x_get_selection, |
Hans Verkuil | da298c6 | 2015-04-09 04:02:34 -0300 | [diff] [blame] | 223 | .set_fmt = ak881x_fill_fmt, |
| 224 | .get_fmt = ak881x_fill_fmt, |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 225 | }; |
| 226 | |
Bhumika Goyal | 4d7aa5e | 2016-12-13 15:26:13 -0200 | [diff] [blame] | 227 | static const struct v4l2_subdev_ops ak881x_subdev_ops = { |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 228 | .core = &ak881x_subdev_core_ops, |
| 229 | .video = &ak881x_subdev_video_ops, |
Hans Verkuil | ebcff5f | 2015-04-09 04:01:33 -0300 | [diff] [blame] | 230 | .pad = &ak881x_subdev_pad_ops, |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | static int ak881x_probe(struct i2c_client *client, |
| 234 | const struct i2c_device_id *did) |
| 235 | { |
| 236 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 237 | struct ak881x *ak881x; |
| 238 | u8 ifmode, data; |
| 239 | |
| 240 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 241 | dev_warn(&adapter->dev, |
| 242 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); |
| 243 | return -EIO; |
| 244 | } |
| 245 | |
Laurent Pinchart | c02b211 | 2013-05-02 08:29:43 -0300 | [diff] [blame] | 246 | ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL); |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 247 | if (!ak881x) |
| 248 | return -ENOMEM; |
| 249 | |
| 250 | v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops); |
| 251 | |
| 252 | data = reg_read(client, AK881X_DEVICE_ID); |
| 253 | |
| 254 | switch (data) { |
| 255 | case 0x13: |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 256 | case 0x14: |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 257 | break; |
| 258 | default: |
| 259 | dev_err(&client->dev, |
| 260 | "No ak881x chip detected, register read %x\n", data); |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 261 | return -ENODEV; |
| 262 | } |
| 263 | |
| 264 | ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION); |
| 265 | ak881x->pdata = client->dev.platform_data; |
| 266 | |
| 267 | if (ak881x->pdata) { |
| 268 | if (ak881x->pdata->flags & AK881X_FIELD) |
| 269 | ifmode = 4; |
| 270 | else |
| 271 | ifmode = 0; |
| 272 | |
| 273 | switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) { |
| 274 | case AK881X_IF_MODE_BT656: |
| 275 | ifmode |= 1; |
| 276 | break; |
| 277 | case AK881X_IF_MODE_MASTER: |
| 278 | ifmode |= 2; |
| 279 | break; |
| 280 | case AK881X_IF_MODE_SLAVE: |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | dev_dbg(&client->dev, "IF mode %x\n", ifmode); |
| 286 | |
| 287 | /* |
| 288 | * "Line Blanking No." seems to be the same as the number of |
| 289 | * "black" lines on, e.g., SuperH VOU, whose default value of 20 |
| 290 | * "incidentally" matches ak881x' default |
| 291 | */ |
| 292 | reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3)); |
| 293 | } |
| 294 | |
| 295 | /* Hardware default: NTSC-M */ |
| 296 | ak881x->lines = 480; |
| 297 | |
| 298 | dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n", |
| 299 | data, ak881x->revision); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int ak881x_remove(struct i2c_client *client) |
| 305 | { |
| 306 | struct ak881x *ak881x = to_ak881x(client); |
| 307 | |
| 308 | v4l2_device_unregister_subdev(&ak881x->subdev); |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static const struct i2c_device_id ak881x_id[] = { |
| 314 | { "ak8813", 0 }, |
| 315 | { "ak8814", 0 }, |
| 316 | { } |
| 317 | }; |
| 318 | MODULE_DEVICE_TABLE(i2c, ak881x_id); |
| 319 | |
| 320 | static struct i2c_driver ak881x_i2c_driver = { |
| 321 | .driver = { |
| 322 | .name = "ak881x", |
| 323 | }, |
| 324 | .probe = ak881x_probe, |
| 325 | .remove = ak881x_remove, |
| 326 | .id_table = ak881x_id, |
| 327 | }; |
| 328 | |
Axel Lin | c6e8d86 | 2012-02-12 06:56:32 -0300 | [diff] [blame] | 329 | module_i2c_driver(ak881x_i2c_driver); |
Guennadi Liakhovetski | aec11e5 | 2010-03-29 04:45:22 -0300 | [diff] [blame] | 330 | |
| 331 | MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814"); |
| 332 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 333 | MODULE_LICENSE("GPL v2"); |