ch7006_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (C) 2009 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include "ch7006_priv.h"
  28. /* DRM encoder functions */
  29. static void ch7006_encoder_set_config(struct drm_encoder *encoder,
  30. void *params)
  31. {
  32. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  33. priv->params = *(struct ch7006_encoder_params *)params;
  34. }
  35. static void ch7006_encoder_destroy(struct drm_encoder *encoder)
  36. {
  37. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  38. drm_property_destroy(encoder->dev, priv->scale_property);
  39. kfree(priv);
  40. to_encoder_slave(encoder)->slave_priv = NULL;
  41. drm_i2c_encoder_destroy(encoder);
  42. }
  43. static void ch7006_encoder_dpms(struct drm_encoder *encoder, int mode)
  44. {
  45. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  46. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  47. struct ch7006_state *state = &priv->state;
  48. ch7006_dbg(client, "\n");
  49. if (mode == priv->last_dpms)
  50. return;
  51. priv->last_dpms = mode;
  52. ch7006_setup_power_state(encoder);
  53. ch7006_load_reg(client, state, CH7006_POWER);
  54. }
  55. static void ch7006_encoder_save(struct drm_encoder *encoder)
  56. {
  57. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  58. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  59. ch7006_dbg(client, "\n");
  60. ch7006_state_save(client, &priv->saved_state);
  61. }
  62. static void ch7006_encoder_restore(struct drm_encoder *encoder)
  63. {
  64. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  65. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  66. ch7006_dbg(client, "\n");
  67. ch7006_state_load(client, &priv->saved_state);
  68. }
  69. static bool ch7006_encoder_mode_fixup(struct drm_encoder *encoder,
  70. const struct drm_display_mode *mode,
  71. struct drm_display_mode *adjusted_mode)
  72. {
  73. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  74. /* The ch7006 is painfully picky with the input timings so no
  75. * custom modes for now... */
  76. priv->mode = ch7006_lookup_mode(encoder, mode);
  77. return !!priv->mode;
  78. }
  79. static int ch7006_encoder_mode_valid(struct drm_encoder *encoder,
  80. struct drm_display_mode *mode)
  81. {
  82. if (ch7006_lookup_mode(encoder, mode))
  83. return MODE_OK;
  84. else
  85. return MODE_BAD;
  86. }
  87. static void ch7006_encoder_mode_set(struct drm_encoder *encoder,
  88. struct drm_display_mode *drm_mode,
  89. struct drm_display_mode *adjusted_mode)
  90. {
  91. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  92. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  93. struct ch7006_encoder_params *params = &priv->params;
  94. struct ch7006_state *state = &priv->state;
  95. uint8_t *regs = state->regs;
  96. const struct ch7006_mode *mode = priv->mode;
  97. const struct ch7006_tv_norm_info *norm = &ch7006_tv_norms[priv->norm];
  98. int start_active;
  99. ch7006_dbg(client, "\n");
  100. regs[CH7006_DISPMODE] = norm->dispmode | mode->dispmode;
  101. regs[CH7006_BWIDTH] = 0;
  102. regs[CH7006_INPUT_FORMAT] = bitf(CH7006_INPUT_FORMAT_FORMAT,
  103. params->input_format);
  104. regs[CH7006_CLKMODE] = CH7006_CLKMODE_SUBC_LOCK
  105. | bitf(CH7006_CLKMODE_XCM, params->xcm)
  106. | bitf(CH7006_CLKMODE_PCM, params->pcm);
  107. if (params->clock_mode)
  108. regs[CH7006_CLKMODE] |= CH7006_CLKMODE_MASTER;
  109. if (params->clock_edge)
  110. regs[CH7006_CLKMODE] |= CH7006_CLKMODE_POS_EDGE;
  111. start_active = (drm_mode->htotal & ~0x7) - (drm_mode->hsync_start & ~0x7);
  112. regs[CH7006_POV] = bitf(CH7006_POV_START_ACTIVE_8, start_active);
  113. regs[CH7006_START_ACTIVE] = bitf(CH7006_START_ACTIVE_0, start_active);
  114. regs[CH7006_INPUT_SYNC] = 0;
  115. if (params->sync_direction)
  116. regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_OUTPUT;
  117. if (params->sync_encoding)
  118. regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_EMBEDDED;
  119. if (drm_mode->flags & DRM_MODE_FLAG_PVSYNC)
  120. regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_PVSYNC;
  121. if (drm_mode->flags & DRM_MODE_FLAG_PHSYNC)
  122. regs[CH7006_INPUT_SYNC] |= CH7006_INPUT_SYNC_PHSYNC;
  123. regs[CH7006_DETECT] = 0;
  124. regs[CH7006_BCLKOUT] = 0;
  125. regs[CH7006_SUBC_INC3] = 0;
  126. if (params->pout_level)
  127. regs[CH7006_SUBC_INC3] |= CH7006_SUBC_INC3_POUT_3_3V;
  128. regs[CH7006_SUBC_INC4] = 0;
  129. if (params->active_detect)
  130. regs[CH7006_SUBC_INC4] |= CH7006_SUBC_INC4_DS_INPUT;
  131. regs[CH7006_PLL_CONTROL] = priv->saved_state.regs[CH7006_PLL_CONTROL];
  132. ch7006_setup_levels(encoder);
  133. ch7006_setup_subcarrier(encoder);
  134. ch7006_setup_pll(encoder);
  135. ch7006_setup_power_state(encoder);
  136. ch7006_setup_properties(encoder);
  137. ch7006_state_load(client, state);
  138. }
  139. static enum drm_connector_status ch7006_encoder_detect(struct drm_encoder *encoder,
  140. struct drm_connector *connector)
  141. {
  142. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  143. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  144. struct ch7006_state *state = &priv->state;
  145. int det;
  146. ch7006_dbg(client, "\n");
  147. ch7006_save_reg(client, state, CH7006_DETECT);
  148. ch7006_save_reg(client, state, CH7006_POWER);
  149. ch7006_save_reg(client, state, CH7006_CLKMODE);
  150. ch7006_write(client, CH7006_POWER, CH7006_POWER_RESET |
  151. bitfs(CH7006_POWER_LEVEL, NORMAL));
  152. ch7006_write(client, CH7006_CLKMODE, CH7006_CLKMODE_MASTER);
  153. ch7006_write(client, CH7006_DETECT, CH7006_DETECT_SENSE);
  154. ch7006_write(client, CH7006_DETECT, 0);
  155. det = ch7006_read(client, CH7006_DETECT);
  156. ch7006_load_reg(client, state, CH7006_CLKMODE);
  157. ch7006_load_reg(client, state, CH7006_POWER);
  158. ch7006_load_reg(client, state, CH7006_DETECT);
  159. if ((det & (CH7006_DETECT_SVIDEO_Y_TEST|
  160. CH7006_DETECT_SVIDEO_C_TEST|
  161. CH7006_DETECT_CVBS_TEST)) == 0)
  162. priv->subconnector = DRM_MODE_SUBCONNECTOR_SCART;
  163. else if ((det & (CH7006_DETECT_SVIDEO_Y_TEST|
  164. CH7006_DETECT_SVIDEO_C_TEST)) == 0)
  165. priv->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO;
  166. else if ((det & CH7006_DETECT_CVBS_TEST) == 0)
  167. priv->subconnector = DRM_MODE_SUBCONNECTOR_Composite;
  168. else
  169. priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  170. drm_object_property_set_value(&connector->base,
  171. encoder->dev->mode_config.tv_subconnector_property,
  172. priv->subconnector);
  173. return priv->subconnector ? connector_status_connected :
  174. connector_status_disconnected;
  175. }
  176. static int ch7006_encoder_get_modes(struct drm_encoder *encoder,
  177. struct drm_connector *connector)
  178. {
  179. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  180. const struct ch7006_mode *mode;
  181. int n = 0;
  182. for (mode = ch7006_modes; mode->mode.clock; mode++) {
  183. if (~mode->valid_scales & 1<<priv->scale ||
  184. ~mode->valid_norms & 1<<priv->norm)
  185. continue;
  186. drm_mode_probed_add(connector,
  187. drm_mode_duplicate(encoder->dev, &mode->mode));
  188. n++;
  189. }
  190. return n;
  191. }
  192. static int ch7006_encoder_create_resources(struct drm_encoder *encoder,
  193. struct drm_connector *connector)
  194. {
  195. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  196. struct drm_device *dev = encoder->dev;
  197. struct drm_mode_config *conf = &dev->mode_config;
  198. drm_mode_create_tv_properties(dev, NUM_TV_NORMS, ch7006_tv_norm_names);
  199. priv->scale_property = drm_property_create_range(dev, 0, "scale", 0, 2);
  200. drm_object_attach_property(&connector->base, conf->tv_select_subconnector_property,
  201. priv->select_subconnector);
  202. drm_object_attach_property(&connector->base, conf->tv_subconnector_property,
  203. priv->subconnector);
  204. drm_object_attach_property(&connector->base, conf->tv_left_margin_property,
  205. priv->hmargin);
  206. drm_object_attach_property(&connector->base, conf->tv_bottom_margin_property,
  207. priv->vmargin);
  208. drm_object_attach_property(&connector->base, conf->tv_mode_property,
  209. priv->norm);
  210. drm_object_attach_property(&connector->base, conf->tv_brightness_property,
  211. priv->brightness);
  212. drm_object_attach_property(&connector->base, conf->tv_contrast_property,
  213. priv->contrast);
  214. drm_object_attach_property(&connector->base, conf->tv_flicker_reduction_property,
  215. priv->flicker);
  216. drm_object_attach_property(&connector->base, priv->scale_property,
  217. priv->scale);
  218. return 0;
  219. }
  220. static int ch7006_encoder_set_property(struct drm_encoder *encoder,
  221. struct drm_connector *connector,
  222. struct drm_property *property,
  223. uint64_t val)
  224. {
  225. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  226. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  227. struct ch7006_state *state = &priv->state;
  228. struct drm_mode_config *conf = &encoder->dev->mode_config;
  229. struct drm_crtc *crtc = encoder->crtc;
  230. bool modes_changed = false;
  231. ch7006_dbg(client, "\n");
  232. if (property == conf->tv_select_subconnector_property) {
  233. priv->select_subconnector = val;
  234. ch7006_setup_power_state(encoder);
  235. ch7006_load_reg(client, state, CH7006_POWER);
  236. } else if (property == conf->tv_left_margin_property) {
  237. priv->hmargin = val;
  238. ch7006_setup_properties(encoder);
  239. ch7006_load_reg(client, state, CH7006_POV);
  240. ch7006_load_reg(client, state, CH7006_HPOS);
  241. } else if (property == conf->tv_bottom_margin_property) {
  242. priv->vmargin = val;
  243. ch7006_setup_properties(encoder);
  244. ch7006_load_reg(client, state, CH7006_POV);
  245. ch7006_load_reg(client, state, CH7006_VPOS);
  246. } else if (property == conf->tv_mode_property) {
  247. if (connector->dpms != DRM_MODE_DPMS_OFF)
  248. return -EINVAL;
  249. priv->norm = val;
  250. modes_changed = true;
  251. } else if (property == conf->tv_brightness_property) {
  252. priv->brightness = val;
  253. ch7006_setup_levels(encoder);
  254. ch7006_load_reg(client, state, CH7006_BLACK_LEVEL);
  255. } else if (property == conf->tv_contrast_property) {
  256. priv->contrast = val;
  257. ch7006_setup_properties(encoder);
  258. ch7006_load_reg(client, state, CH7006_CONTRAST);
  259. } else if (property == conf->tv_flicker_reduction_property) {
  260. priv->flicker = val;
  261. ch7006_setup_properties(encoder);
  262. ch7006_load_reg(client, state, CH7006_FFILTER);
  263. } else if (property == priv->scale_property) {
  264. if (connector->dpms != DRM_MODE_DPMS_OFF)
  265. return -EINVAL;
  266. priv->scale = val;
  267. modes_changed = true;
  268. } else {
  269. return -EINVAL;
  270. }
  271. if (modes_changed) {
  272. drm_helper_probe_single_connector_modes(connector, 0, 0);
  273. /* Disable the crtc to ensure a full modeset is
  274. * performed whenever it's turned on again. */
  275. if (crtc) {
  276. struct drm_mode_set modeset = {
  277. .crtc = crtc,
  278. };
  279. drm_mode_set_config_internal(&modeset);
  280. }
  281. }
  282. return 0;
  283. }
  284. static struct drm_encoder_slave_funcs ch7006_encoder_funcs = {
  285. .set_config = ch7006_encoder_set_config,
  286. .destroy = ch7006_encoder_destroy,
  287. .dpms = ch7006_encoder_dpms,
  288. .save = ch7006_encoder_save,
  289. .restore = ch7006_encoder_restore,
  290. .mode_fixup = ch7006_encoder_mode_fixup,
  291. .mode_valid = ch7006_encoder_mode_valid,
  292. .mode_set = ch7006_encoder_mode_set,
  293. .detect = ch7006_encoder_detect,
  294. .get_modes = ch7006_encoder_get_modes,
  295. .create_resources = ch7006_encoder_create_resources,
  296. .set_property = ch7006_encoder_set_property,
  297. };
  298. /* I2C driver functions */
  299. static int ch7006_probe(struct i2c_client *client, const struct i2c_device_id *id)
  300. {
  301. uint8_t addr = CH7006_VERSION_ID;
  302. uint8_t val;
  303. int ret;
  304. ch7006_dbg(client, "\n");
  305. ret = i2c_master_send(client, &addr, sizeof(addr));
  306. if (ret < 0)
  307. goto fail;
  308. ret = i2c_master_recv(client, &val, sizeof(val));
  309. if (ret < 0)
  310. goto fail;
  311. ch7006_info(client, "Detected version ID: %x\n", val);
  312. /* I don't know what this is for, but otherwise I get no
  313. * signal.
  314. */
  315. ch7006_write(client, 0x3d, 0x0);
  316. return 0;
  317. fail:
  318. ch7006_err(client, "Error %d reading version ID\n", ret);
  319. return -ENODEV;
  320. }
  321. static int ch7006_remove(struct i2c_client *client)
  322. {
  323. ch7006_dbg(client, "\n");
  324. return 0;
  325. }
  326. static int ch7006_resume(struct device *dev)
  327. {
  328. struct i2c_client *client = to_i2c_client(dev);
  329. ch7006_dbg(client, "\n");
  330. ch7006_write(client, 0x3d, 0x0);
  331. return 0;
  332. }
  333. static int ch7006_encoder_init(struct i2c_client *client,
  334. struct drm_device *dev,
  335. struct drm_encoder_slave *encoder)
  336. {
  337. struct ch7006_priv *priv;
  338. int i;
  339. ch7006_dbg(client, "\n");
  340. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  341. if (!priv)
  342. return -ENOMEM;
  343. encoder->slave_priv = priv;
  344. encoder->slave_funcs = &ch7006_encoder_funcs;
  345. priv->norm = TV_NORM_PAL;
  346. priv->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
  347. priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  348. priv->scale = 1;
  349. priv->contrast = 50;
  350. priv->brightness = 50;
  351. priv->flicker = 50;
  352. priv->hmargin = 50;
  353. priv->vmargin = 50;
  354. priv->last_dpms = -1;
  355. priv->chip_version = ch7006_read(client, CH7006_VERSION_ID);
  356. if (ch7006_tv_norm) {
  357. for (i = 0; i < NUM_TV_NORMS; i++) {
  358. if (!strcmp(ch7006_tv_norm_names[i], ch7006_tv_norm)) {
  359. priv->norm = i;
  360. break;
  361. }
  362. }
  363. if (i == NUM_TV_NORMS)
  364. ch7006_err(client, "Invalid TV norm setting \"%s\".\n",
  365. ch7006_tv_norm);
  366. }
  367. if (ch7006_scale >= 0 && ch7006_scale <= 2)
  368. priv->scale = ch7006_scale;
  369. else
  370. ch7006_err(client, "Invalid scale setting \"%d\".\n",
  371. ch7006_scale);
  372. return 0;
  373. }
  374. static struct i2c_device_id ch7006_ids[] = {
  375. { "ch7006", 0 },
  376. { }
  377. };
  378. MODULE_DEVICE_TABLE(i2c, ch7006_ids);
  379. static const struct dev_pm_ops ch7006_pm_ops = {
  380. .resume = ch7006_resume,
  381. };
  382. static struct drm_i2c_encoder_driver ch7006_driver = {
  383. .i2c_driver = {
  384. .probe = ch7006_probe,
  385. .remove = ch7006_remove,
  386. .driver = {
  387. .name = "ch7006",
  388. .pm = &ch7006_pm_ops,
  389. },
  390. .id_table = ch7006_ids,
  391. },
  392. .encoder_init = ch7006_encoder_init,
  393. };
  394. /* Module initialization */
  395. static int __init ch7006_init(void)
  396. {
  397. return drm_i2c_encoder_register(THIS_MODULE, &ch7006_driver);
  398. }
  399. static void __exit ch7006_exit(void)
  400. {
  401. drm_i2c_encoder_unregister(&ch7006_driver);
  402. }
  403. int ch7006_debug;
  404. module_param_named(debug, ch7006_debug, int, 0600);
  405. MODULE_PARM_DESC(debug, "Enable debug output.");
  406. char *ch7006_tv_norm;
  407. module_param_named(tv_norm, ch7006_tv_norm, charp, 0600);
  408. MODULE_PARM_DESC(tv_norm, "Default TV norm.\n"
  409. "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, PAL-60, NTSC-M, NTSC-J.\n"
  410. "\t\tDefault: PAL");
  411. int ch7006_scale = 1;
  412. module_param_named(scale, ch7006_scale, int, 0600);
  413. MODULE_PARM_DESC(scale, "Default scale.\n"
  414. "\t\tSupported: 0 -> Select video modes with a higher blanking ratio.\n"
  415. "\t\t\t1 -> Select default video modes.\n"
  416. "\t\t\t2 -> Select video modes with a lower blanking ratio.");
  417. MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
  418. MODULE_DESCRIPTION("Chrontel ch7006 TV encoder driver");
  419. MODULE_LICENSE("GPL and additional rights");
  420. module_init(ch7006_init);
  421. module_exit(ch7006_exit);