ak881x.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Driver for AK8813 / AK8814 TV-ecoders from Asahi Kasei Microsystems Co., Ltd. (AKM)
  3. *
  4. * Copyright (C) 2010, Guennadi Liakhovetski <[email protected]>
  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/i2c/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_get_selection(struct v4l2_subdev *sd,
  104. struct v4l2_subdev_pad_config *cfg,
  105. struct v4l2_subdev_selection *sel)
  106. {
  107. struct i2c_client *client = v4l2_get_subdevdata(sd);
  108. struct ak881x *ak881x = to_ak881x(client);
  109. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  110. return -EINVAL;
  111. switch (sel->target) {
  112. case V4L2_SEL_TGT_CROP_BOUNDS:
  113. case V4L2_SEL_TGT_CROP_DEFAULT:
  114. sel->r.left = 0;
  115. sel->r.top = 0;
  116. sel->r.width = 720;
  117. sel->r.height = ak881x->lines;
  118. return 0;
  119. default:
  120. return -EINVAL;
  121. }
  122. }
  123. static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  124. {
  125. struct i2c_client *client = v4l2_get_subdevdata(sd);
  126. struct ak881x *ak881x = to_ak881x(client);
  127. u8 vp1;
  128. if (std == V4L2_STD_NTSC_443) {
  129. vp1 = 3;
  130. ak881x->lines = 480;
  131. } else if (std == V4L2_STD_PAL_M) {
  132. vp1 = 5;
  133. ak881x->lines = 480;
  134. } else if (std == V4L2_STD_PAL_60) {
  135. vp1 = 7;
  136. ak881x->lines = 480;
  137. } else if (std & V4L2_STD_NTSC) {
  138. vp1 = 0;
  139. ak881x->lines = 480;
  140. } else if (std & V4L2_STD_PAL) {
  141. vp1 = 0xf;
  142. ak881x->lines = 576;
  143. } else {
  144. /* No SECAM or PAL_N/Nc supported */
  145. return -EINVAL;
  146. }
  147. reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
  148. return 0;
  149. }
  150. static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
  151. {
  152. struct i2c_client *client = v4l2_get_subdevdata(sd);
  153. struct ak881x *ak881x = to_ak881x(client);
  154. if (enable) {
  155. u8 dac;
  156. /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
  157. /* Default: composite output */
  158. if (ak881x->pdata->flags & AK881X_COMPONENT)
  159. dac = 3;
  160. else
  161. dac = 4;
  162. /* Turn on the DAC(s) */
  163. reg_write(client, AK881X_DAC_MODE, dac);
  164. dev_dbg(&client->dev, "chip status 0x%x\n",
  165. reg_read(client, AK881X_STATUS));
  166. } else {
  167. /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
  168. reg_write(client, AK881X_DAC_MODE, 0);
  169. dev_dbg(&client->dev, "chip status 0x%x\n",
  170. reg_read(client, AK881X_STATUS));
  171. }
  172. return 0;
  173. }
  174. static struct v4l2_subdev_core_ops ak881x_subdev_core_ops = {
  175. #ifdef CONFIG_VIDEO_ADV_DEBUG
  176. .g_register = ak881x_g_register,
  177. .s_register = ak881x_s_register,
  178. #endif
  179. };
  180. static struct v4l2_subdev_video_ops ak881x_subdev_video_ops = {
  181. .s_std_output = ak881x_s_std_output,
  182. .s_stream = ak881x_s_stream,
  183. };
  184. static const struct v4l2_subdev_pad_ops ak881x_subdev_pad_ops = {
  185. .enum_mbus_code = ak881x_enum_mbus_code,
  186. .get_selection = ak881x_get_selection,
  187. .set_fmt = ak881x_fill_fmt,
  188. .get_fmt = ak881x_fill_fmt,
  189. };
  190. static struct v4l2_subdev_ops ak881x_subdev_ops = {
  191. .core = &ak881x_subdev_core_ops,
  192. .video = &ak881x_subdev_video_ops,
  193. .pad = &ak881x_subdev_pad_ops,
  194. };
  195. static int ak881x_probe(struct i2c_client *client,
  196. const struct i2c_device_id *did)
  197. {
  198. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  199. struct ak881x *ak881x;
  200. u8 ifmode, data;
  201. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  202. dev_warn(&adapter->dev,
  203. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  204. return -EIO;
  205. }
  206. ak881x = devm_kzalloc(&client->dev, sizeof(*ak881x), GFP_KERNEL);
  207. if (!ak881x)
  208. return -ENOMEM;
  209. v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
  210. data = reg_read(client, AK881X_DEVICE_ID);
  211. switch (data) {
  212. case 0x13:
  213. case 0x14:
  214. break;
  215. default:
  216. dev_err(&client->dev,
  217. "No ak881x chip detected, register read %x\n", data);
  218. return -ENODEV;
  219. }
  220. ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
  221. ak881x->pdata = client->dev.platform_data;
  222. if (ak881x->pdata) {
  223. if (ak881x->pdata->flags & AK881X_FIELD)
  224. ifmode = 4;
  225. else
  226. ifmode = 0;
  227. switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
  228. case AK881X_IF_MODE_BT656:
  229. ifmode |= 1;
  230. break;
  231. case AK881X_IF_MODE_MASTER:
  232. ifmode |= 2;
  233. break;
  234. case AK881X_IF_MODE_SLAVE:
  235. default:
  236. break;
  237. }
  238. dev_dbg(&client->dev, "IF mode %x\n", ifmode);
  239. /*
  240. * "Line Blanking No." seems to be the same as the number of
  241. * "black" lines on, e.g., SuperH VOU, whose default value of 20
  242. * "incidentally" matches ak881x' default
  243. */
  244. reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
  245. }
  246. /* Hardware default: NTSC-M */
  247. ak881x->lines = 480;
  248. dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
  249. data, ak881x->revision);
  250. return 0;
  251. }
  252. static int ak881x_remove(struct i2c_client *client)
  253. {
  254. struct ak881x *ak881x = to_ak881x(client);
  255. v4l2_device_unregister_subdev(&ak881x->subdev);
  256. return 0;
  257. }
  258. static const struct i2c_device_id ak881x_id[] = {
  259. { "ak8813", 0 },
  260. { "ak8814", 0 },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(i2c, ak881x_id);
  264. static struct i2c_driver ak881x_i2c_driver = {
  265. .driver = {
  266. .name = "ak881x",
  267. },
  268. .probe = ak881x_probe,
  269. .remove = ak881x_remove,
  270. .id_table = ak881x_id,
  271. };
  272. module_i2c_driver(ak881x_i2c_driver);
  273. MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
  274. MODULE_AUTHOR("Guennadi Liakhovetski <[email protected]>");
  275. MODULE_LICENSE("GPL v2");