ak881x.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_try_g_mbus_fmt(struct v4l2_subdev *sd,
  79. struct v4l2_mbus_framefmt *mf)
  80. {
  81. struct i2c_client *client = v4l2_get_subdevdata(sd);
  82. struct ak881x *ak881x = to_ak881x(client);
  83. v4l_bound_align_image(&mf->width, 0, 720, 2,
  84. &mf->height, 0, ak881x->lines, 1, 0);
  85. mf->field = V4L2_FIELD_INTERLACED;
  86. mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
  87. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  88. return 0;
  89. }
  90. static int ak881x_s_mbus_fmt(struct v4l2_subdev *sd,
  91. struct v4l2_mbus_framefmt *mf)
  92. {
  93. if (mf->field != V4L2_FIELD_INTERLACED ||
  94. mf->code != V4L2_MBUS_FMT_YUYV8_2X8)
  95. return -EINVAL;
  96. return ak881x_try_g_mbus_fmt(sd, mf);
  97. }
  98. static int ak881x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  99. enum v4l2_mbus_pixelcode *code)
  100. {
  101. if (index)
  102. return -EINVAL;
  103. *code = V4L2_MBUS_FMT_YUYV8_2X8;
  104. return 0;
  105. }
  106. static int ak881x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  107. {
  108. struct i2c_client *client = v4l2_get_subdevdata(sd);
  109. struct ak881x *ak881x = to_ak881x(client);
  110. a->bounds.left = 0;
  111. a->bounds.top = 0;
  112. a->bounds.width = 720;
  113. a->bounds.height = ak881x->lines;
  114. a->defrect = a->bounds;
  115. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  116. a->pixelaspect.numerator = 1;
  117. a->pixelaspect.denominator = 1;
  118. return 0;
  119. }
  120. static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  121. {
  122. struct i2c_client *client = v4l2_get_subdevdata(sd);
  123. struct ak881x *ak881x = to_ak881x(client);
  124. u8 vp1;
  125. if (std == V4L2_STD_NTSC_443) {
  126. vp1 = 3;
  127. ak881x->lines = 480;
  128. } else if (std == V4L2_STD_PAL_M) {
  129. vp1 = 5;
  130. ak881x->lines = 480;
  131. } else if (std == V4L2_STD_PAL_60) {
  132. vp1 = 7;
  133. ak881x->lines = 480;
  134. } else if (std && !(std & ~V4L2_STD_PAL)) {
  135. vp1 = 0xf;
  136. ak881x->lines = 576;
  137. } else if (std && !(std & ~V4L2_STD_NTSC)) {
  138. vp1 = 0;
  139. ak881x->lines = 480;
  140. } else {
  141. /* No SECAM or PAL_N/Nc supported */
  142. return -EINVAL;
  143. }
  144. reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
  145. return 0;
  146. }
  147. static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
  148. {
  149. struct i2c_client *client = v4l2_get_subdevdata(sd);
  150. struct ak881x *ak881x = to_ak881x(client);
  151. if (enable) {
  152. u8 dac;
  153. /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
  154. /* Default: composite output */
  155. if (ak881x->pdata->flags & AK881X_COMPONENT)
  156. dac = 3;
  157. else
  158. dac = 4;
  159. /* Turn on the DAC(s) */
  160. reg_write(client, AK881X_DAC_MODE, dac);
  161. dev_dbg(&client->dev, "chip status 0x%x\n",
  162. reg_read(client, AK881X_STATUS));
  163. } else {
  164. /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
  165. reg_write(client, AK881X_DAC_MODE, 0);
  166. dev_dbg(&client->dev, "chip status 0x%x\n",
  167. reg_read(client, AK881X_STATUS));
  168. }
  169. return 0;
  170. }
  171. static struct v4l2_subdev_core_ops ak881x_subdev_core_ops = {
  172. #ifdef CONFIG_VIDEO_ADV_DEBUG
  173. .g_register = ak881x_g_register,
  174. .s_register = ak881x_s_register,
  175. #endif
  176. };
  177. static struct v4l2_subdev_video_ops ak881x_subdev_video_ops = {
  178. .s_mbus_fmt = ak881x_s_mbus_fmt,
  179. .g_mbus_fmt = ak881x_try_g_mbus_fmt,
  180. .try_mbus_fmt = ak881x_try_g_mbus_fmt,
  181. .cropcap = ak881x_cropcap,
  182. .enum_mbus_fmt = ak881x_enum_mbus_fmt,
  183. .s_std_output = ak881x_s_std_output,
  184. .s_stream = ak881x_s_stream,
  185. };
  186. static struct v4l2_subdev_ops ak881x_subdev_ops = {
  187. .core = &ak881x_subdev_core_ops,
  188. .video = &ak881x_subdev_video_ops,
  189. };
  190. static int ak881x_probe(struct i2c_client *client,
  191. const struct i2c_device_id *did)
  192. {
  193. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  194. struct ak881x *ak881x;
  195. u8 ifmode, data;
  196. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  197. dev_warn(&adapter->dev,
  198. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  199. return -EIO;
  200. }
  201. ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL);
  202. if (!ak881x)
  203. return -ENOMEM;
  204. v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
  205. data = reg_read(client, AK881X_DEVICE_ID);
  206. switch (data) {
  207. case 0x13:
  208. case 0x14:
  209. break;
  210. default:
  211. dev_err(&client->dev,
  212. "No ak881x chip detected, register read %x\n", data);
  213. return -ENODEV;
  214. }
  215. ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
  216. ak881x->pdata = client->dev.platform_data;
  217. if (ak881x->pdata) {
  218. if (ak881x->pdata->flags & AK881X_FIELD)
  219. ifmode = 4;
  220. else
  221. ifmode = 0;
  222. switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
  223. case AK881X_IF_MODE_BT656:
  224. ifmode |= 1;
  225. break;
  226. case AK881X_IF_MODE_MASTER:
  227. ifmode |= 2;
  228. break;
  229. case AK881X_IF_MODE_SLAVE:
  230. default:
  231. break;
  232. }
  233. dev_dbg(&client->dev, "IF mode %x\n", ifmode);
  234. /*
  235. * "Line Blanking No." seems to be the same as the number of
  236. * "black" lines on, e.g., SuperH VOU, whose default value of 20
  237. * "incidentally" matches ak881x' default
  238. */
  239. reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
  240. }
  241. /* Hardware default: NTSC-M */
  242. ak881x->lines = 480;
  243. dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
  244. data, ak881x->revision);
  245. return 0;
  246. }
  247. static int ak881x_remove(struct i2c_client *client)
  248. {
  249. struct ak881x *ak881x = to_ak881x(client);
  250. v4l2_device_unregister_subdev(&ak881x->subdev);
  251. return 0;
  252. }
  253. static const struct i2c_device_id ak881x_id[] = {
  254. { "ak8813", 0 },
  255. { "ak8814", 0 },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(i2c, ak881x_id);
  259. static struct i2c_driver ak881x_i2c_driver = {
  260. .driver = {
  261. .name = "ak881x",
  262. },
  263. .probe = ak881x_probe,
  264. .remove = ak881x_remove,
  265. .id_table = ak881x_id,
  266. };
  267. module_i2c_driver(ak881x_i2c_driver);
  268. MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
  269. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  270. MODULE_LICENSE("GPL v2");