ak881x.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/videodev2.h>
  15. #include <linux/module.h>
  16. #include <media/ak881x.h>
  17. #include <media/v4l2-common.h>
  18. #include <media/v4l2-device.h>
  19. #define AK881X_INTERFACE_MODE 0
  20. #define AK881X_VIDEO_PROCESS1 1
  21. #define AK881X_VIDEO_PROCESS2 2
  22. #define AK881X_VIDEO_PROCESS3 3
  23. #define AK881X_DAC_MODE 5
  24. #define AK881X_STATUS 0x24
  25. #define AK881X_DEVICE_ID 0x25
  26. #define AK881X_DEVICE_REVISION 0x26
  27. struct ak881x {
  28. struct v4l2_subdev subdev;
  29. struct ak881x_pdata *pdata;
  30. unsigned int lines;
  31. char revision; /* DEVICE_REVISION content */
  32. };
  33. static int reg_read(struct i2c_client *client, const u8 reg)
  34. {
  35. return i2c_smbus_read_byte_data(client, reg);
  36. }
  37. static int reg_write(struct i2c_client *client, const u8 reg,
  38. const u8 data)
  39. {
  40. return i2c_smbus_write_byte_data(client, reg, data);
  41. }
  42. static int reg_set(struct i2c_client *client, const u8 reg,
  43. const u8 data, u8 mask)
  44. {
  45. int ret = reg_read(client, reg);
  46. if (ret < 0)
  47. return ret;
  48. return reg_write(client, reg, (ret & ~mask) | (data & mask));
  49. }
  50. static struct ak881x *to_ak881x(const struct i2c_client *client)
  51. {
  52. return container_of(i2c_get_clientdata(client), struct ak881x, subdev);
  53. }
  54. #ifdef CONFIG_VIDEO_ADV_DEBUG
  55. static int ak881x_g_register(struct v4l2_subdev *sd,
  56. struct v4l2_dbg_register *reg)
  57. {
  58. struct i2c_client *client = v4l2_get_subdevdata(sd);
  59. if (reg->reg > 0x26)
  60. return -EINVAL;
  61. reg->size = 1;
  62. reg->val = reg_read(client, reg->reg);
  63. if (reg->val > 0xffff)
  64. return -EIO;
  65. return 0;
  66. }
  67. static int ak881x_s_register(struct v4l2_subdev *sd,
  68. const struct v4l2_dbg_register *reg)
  69. {
  70. struct i2c_client *client = v4l2_get_subdevdata(sd);
  71. if (reg->reg > 0x26)
  72. return -EINVAL;
  73. if (reg_write(client, reg->reg, reg->val) < 0)
  74. return -EIO;
  75. return 0;
  76. }
  77. #endif
  78. static int ak881x_fill_fmt(struct v4l2_subdev *sd,
  79. struct v4l2_subdev_pad_config *cfg,
  80. struct v4l2_subdev_format *format)
  81. {
  82. struct v4l2_mbus_framefmt *mf = &format->format;
  83. struct i2c_client *client = v4l2_get_subdevdata(sd);
  84. struct ak881x *ak881x = to_ak881x(client);
  85. if (format->pad)
  86. return -EINVAL;
  87. v4l_bound_align_image(&mf->width, 0, 720, 2,
  88. &mf->height, 0, ak881x->lines, 1, 0);
  89. mf->field = V4L2_FIELD_INTERLACED;
  90. mf->code = MEDIA_BUS_FMT_YUYV8_2X8;
  91. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  92. return 0;
  93. }
  94. static int ak881x_enum_mbus_code(struct v4l2_subdev *sd,
  95. struct v4l2_subdev_pad_config *cfg,
  96. struct v4l2_subdev_mbus_code_enum *code)
  97. {
  98. if (code->pad || code->index)
  99. return -EINVAL;
  100. code->code = MEDIA_BUS_FMT_YUYV8_2X8;
  101. return 0;
  102. }
  103. static int ak881x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  104. {
  105. struct i2c_client *client = v4l2_get_subdevdata(sd);
  106. struct ak881x *ak881x = to_ak881x(client);
  107. a->bounds.left = 0;
  108. a->bounds.top = 0;
  109. a->bounds.width = 720;
  110. a->bounds.height = ak881x->lines;
  111. a->defrect = a->bounds;
  112. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  113. a->pixelaspect.numerator = 1;
  114. a->pixelaspect.denominator = 1;
  115. return 0;
  116. }
  117. static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  118. {
  119. struct i2c_client *client = v4l2_get_subdevdata(sd);
  120. struct ak881x *ak881x = to_ak881x(client);
  121. u8 vp1;
  122. if (std == V4L2_STD_NTSC_443) {
  123. vp1 = 3;
  124. ak881x->lines = 480;
  125. } else if (std == V4L2_STD_PAL_M) {
  126. vp1 = 5;
  127. ak881x->lines = 480;
  128. } else if (std == V4L2_STD_PAL_60) {
  129. vp1 = 7;
  130. ak881x->lines = 480;
  131. } else if (std & V4L2_STD_NTSC) {
  132. vp1 = 0;
  133. ak881x->lines = 480;
  134. } else if (std & V4L2_STD_PAL) {
  135. vp1 = 0xf;
  136. ak881x->lines = 576;
  137. } else {
  138. /* No SECAM or PAL_N/Nc supported */
  139. return -EINVAL;
  140. }
  141. reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
  142. return 0;
  143. }
  144. static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
  145. {
  146. struct i2c_client *client = v4l2_get_subdevdata(sd);
  147. struct ak881x *ak881x = to_ak881x(client);
  148. if (enable) {
  149. u8 dac;
  150. /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
  151. /* Default: composite output */
  152. if (ak881x->pdata->flags & AK881X_COMPONENT)
  153. dac = 3;
  154. else
  155. dac = 4;
  156. /* Turn on the DAC(s) */
  157. reg_write(client, AK881X_DAC_MODE, dac);
  158. dev_dbg(&client->dev, "chip status 0x%x\n",
  159. reg_read(client, AK881X_STATUS));
  160. } else {
  161. /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
  162. reg_write(client, AK881X_DAC_MODE, 0);
  163. dev_dbg(&client->dev, "chip status 0x%x\n",
  164. reg_read(client, AK881X_STATUS));
  165. }
  166. return 0;
  167. }
  168. static struct v4l2_subdev_core_ops ak881x_subdev_core_ops = {
  169. #ifdef CONFIG_VIDEO_ADV_DEBUG
  170. .g_register = ak881x_g_register,
  171. .s_register = ak881x_s_register,
  172. #endif
  173. };
  174. static struct v4l2_subdev_video_ops ak881x_subdev_video_ops = {
  175. .cropcap = ak881x_cropcap,
  176. .s_std_output = ak881x_s_std_output,
  177. .s_stream = ak881x_s_stream,
  178. };
  179. static const struct v4l2_subdev_pad_ops ak881x_subdev_pad_ops = {
  180. .enum_mbus_code = ak881x_enum_mbus_code,
  181. .set_fmt = ak881x_fill_fmt,
  182. .get_fmt = ak881x_fill_fmt,
  183. };
  184. static struct v4l2_subdev_ops ak881x_subdev_ops = {
  185. .core = &ak881x_subdev_core_ops,
  186. .video = &ak881x_subdev_video_ops,
  187. .pad = &ak881x_subdev_pad_ops,
  188. };
  189. static int ak881x_probe(struct i2c_client *client,
  190. const struct i2c_device_id *did)
  191. {
  192. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  193. struct ak881x *ak881x;
  194. u8 ifmode, data;
  195. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  196. dev_warn(&adapter->dev,
  197. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  198. return -EIO;
  199. }
  200. ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL);
  201. if (!ak881x)
  202. return -ENOMEM;
  203. v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
  204. data = reg_read(client, AK881X_DEVICE_ID);
  205. switch (data) {
  206. case 0x13:
  207. case 0x14:
  208. break;
  209. default:
  210. dev_err(&client->dev,
  211. "No ak881x chip detected, register read %x\n", data);
  212. return -ENODEV;
  213. }
  214. ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
  215. ak881x->pdata = client->dev.platform_data;
  216. if (ak881x->pdata) {
  217. if (ak881x->pdata->flags & AK881X_FIELD)
  218. ifmode = 4;
  219. else
  220. ifmode = 0;
  221. switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
  222. case AK881X_IF_MODE_BT656:
  223. ifmode |= 1;
  224. break;
  225. case AK881X_IF_MODE_MASTER:
  226. ifmode |= 2;
  227. break;
  228. case AK881X_IF_MODE_SLAVE:
  229. default:
  230. break;
  231. }
  232. dev_dbg(&client->dev, "IF mode %x\n", ifmode);
  233. /*
  234. * "Line Blanking No." seems to be the same as the number of
  235. * "black" lines on, e.g., SuperH VOU, whose default value of 20
  236. * "incidentally" matches ak881x' default
  237. */
  238. reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
  239. }
  240. /* Hardware default: NTSC-M */
  241. ak881x->lines = 480;
  242. dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
  243. data, ak881x->revision);
  244. return 0;
  245. }
  246. static int ak881x_remove(struct i2c_client *client)
  247. {
  248. struct ak881x *ak881x = to_ak881x(client);
  249. v4l2_device_unregister_subdev(&ak881x->subdev);
  250. return 0;
  251. }
  252. static const struct i2c_device_id ak881x_id[] = {
  253. { "ak8813", 0 },
  254. { "ak8814", 0 },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(i2c, ak881x_id);
  258. static struct i2c_driver ak881x_i2c_driver = {
  259. .driver = {
  260. .name = "ak881x",
  261. },
  262. .probe = ak881x_probe,
  263. .remove = ak881x_remove,
  264. .id_table = ak881x_id,
  265. };
  266. module_i2c_driver(ak881x_i2c_driver);
  267. MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
  268. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  269. MODULE_LICENSE("GPL v2");