ak881x.c 8.5 KB

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