ch7006_drv.c 15 KB

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