ch7006_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. 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. struct ch7006_mode *mode = priv->mode;
  97. 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_connector_property_set_value(connector,
  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. 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(dev, DRM_MODE_PROP_RANGE,
  200. "scale", 2);
  201. priv->scale_property->values[0] = 0;
  202. priv->scale_property->values[1] = 2;
  203. drm_connector_attach_property(connector, conf->tv_select_subconnector_property,
  204. priv->select_subconnector);
  205. drm_connector_attach_property(connector, conf->tv_subconnector_property,
  206. priv->subconnector);
  207. drm_connector_attach_property(connector, conf->tv_left_margin_property,
  208. priv->hmargin);
  209. drm_connector_attach_property(connector, conf->tv_bottom_margin_property,
  210. priv->vmargin);
  211. drm_connector_attach_property(connector, conf->tv_mode_property,
  212. priv->norm);
  213. drm_connector_attach_property(connector, conf->tv_brightness_property,
  214. priv->brightness);
  215. drm_connector_attach_property(connector, conf->tv_contrast_property,
  216. priv->contrast);
  217. drm_connector_attach_property(connector, conf->tv_flicker_reduction_property,
  218. priv->flicker);
  219. drm_connector_attach_property(connector, priv->scale_property,
  220. priv->scale);
  221. return 0;
  222. }
  223. static int ch7006_encoder_set_property(struct drm_encoder *encoder,
  224. struct drm_connector *connector,
  225. struct drm_property *property,
  226. uint64_t val)
  227. {
  228. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  229. struct ch7006_priv *priv = to_ch7006_priv(encoder);
  230. struct ch7006_state *state = &priv->state;
  231. struct drm_mode_config *conf = &encoder->dev->mode_config;
  232. struct drm_crtc *crtc = encoder->crtc;
  233. bool modes_changed = false;
  234. ch7006_dbg(client, "\n");
  235. if (property == conf->tv_select_subconnector_property) {
  236. priv->select_subconnector = val;
  237. ch7006_setup_power_state(encoder);
  238. ch7006_load_reg(client, state, CH7006_POWER);
  239. } else if (property == conf->tv_left_margin_property) {
  240. priv->hmargin = val;
  241. ch7006_setup_properties(encoder);
  242. ch7006_load_reg(client, state, CH7006_POV);
  243. ch7006_load_reg(client, state, CH7006_HPOS);
  244. } else if (property == conf->tv_bottom_margin_property) {
  245. priv->vmargin = val;
  246. ch7006_setup_properties(encoder);
  247. ch7006_load_reg(client, state, CH7006_POV);
  248. ch7006_load_reg(client, state, CH7006_VPOS);
  249. } else if (property == conf->tv_mode_property) {
  250. if (connector->dpms != DRM_MODE_DPMS_OFF)
  251. return -EINVAL;
  252. priv->norm = val;
  253. modes_changed = true;
  254. } else if (property == conf->tv_brightness_property) {
  255. priv->brightness = val;
  256. ch7006_setup_levels(encoder);
  257. ch7006_load_reg(client, state, CH7006_BLACK_LEVEL);
  258. } else if (property == conf->tv_contrast_property) {
  259. priv->contrast = val;
  260. ch7006_setup_properties(encoder);
  261. ch7006_load_reg(client, state, CH7006_CONTRAST);
  262. } else if (property == conf->tv_flicker_reduction_property) {
  263. priv->flicker = val;
  264. ch7006_setup_properties(encoder);
  265. ch7006_load_reg(client, state, CH7006_FFILTER);
  266. } else if (property == priv->scale_property) {
  267. if (connector->dpms != DRM_MODE_DPMS_OFF)
  268. return -EINVAL;
  269. priv->scale = val;
  270. modes_changed = true;
  271. } else {
  272. return -EINVAL;
  273. }
  274. if (modes_changed) {
  275. drm_helper_probe_single_connector_modes(connector, 0, 0);
  276. /* Disable the crtc to ensure a full modeset is
  277. * performed whenever it's turned on again. */
  278. if (crtc) {
  279. struct drm_mode_set modeset = {
  280. .crtc = crtc,
  281. };
  282. crtc->funcs->set_config(&modeset);
  283. }
  284. }
  285. return 0;
  286. }
  287. static struct drm_encoder_slave_funcs ch7006_encoder_funcs = {
  288. .set_config = ch7006_encoder_set_config,
  289. .destroy = ch7006_encoder_destroy,
  290. .dpms = ch7006_encoder_dpms,
  291. .save = ch7006_encoder_save,
  292. .restore = ch7006_encoder_restore,
  293. .mode_fixup = ch7006_encoder_mode_fixup,
  294. .mode_valid = ch7006_encoder_mode_valid,
  295. .mode_set = ch7006_encoder_mode_set,
  296. .detect = ch7006_encoder_detect,
  297. .get_modes = ch7006_encoder_get_modes,
  298. .create_resources = ch7006_encoder_create_resources,
  299. .set_property = ch7006_encoder_set_property,
  300. };
  301. /* I2C driver functions */
  302. static int ch7006_probe(struct i2c_client *client, const struct i2c_device_id *id)
  303. {
  304. uint8_t addr = CH7006_VERSION_ID;
  305. uint8_t val;
  306. int ret;
  307. ch7006_dbg(client, "\n");
  308. ret = i2c_master_send(client, &addr, sizeof(addr));
  309. if (ret < 0)
  310. goto fail;
  311. ret = i2c_master_recv(client, &val, sizeof(val));
  312. if (ret < 0)
  313. goto fail;
  314. ch7006_info(client, "Detected version ID: %x\n", val);
  315. /* I don't know what this is for, but otherwise I get no
  316. * signal.
  317. */
  318. ch7006_write(client, 0x3d, 0x0);
  319. return 0;
  320. fail:
  321. ch7006_err(client, "Error %d reading version ID\n", ret);
  322. return -ENODEV;
  323. }
  324. static int ch7006_remove(struct i2c_client *client)
  325. {
  326. ch7006_dbg(client, "\n");
  327. return 0;
  328. }
  329. static int ch7006_suspend(struct i2c_client *client, pm_message_t mesg)
  330. {
  331. ch7006_dbg(client, "\n");
  332. return 0;
  333. }
  334. static int ch7006_resume(struct i2c_client *client)
  335. {
  336. ch7006_dbg(client, "\n");
  337. ch7006_write(client, 0x3d, 0x0);
  338. return 0;
  339. }
  340. static int ch7006_encoder_init(struct i2c_client *client,
  341. struct drm_device *dev,
  342. struct drm_encoder_slave *encoder)
  343. {
  344. struct ch7006_priv *priv;
  345. int i;
  346. ch7006_dbg(client, "\n");
  347. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  348. if (!priv)
  349. return -ENOMEM;
  350. encoder->slave_priv = priv;
  351. encoder->slave_funcs = &ch7006_encoder_funcs;
  352. priv->norm = TV_NORM_PAL;
  353. priv->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
  354. priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  355. priv->scale = 1;
  356. priv->contrast = 50;
  357. priv->brightness = 50;
  358. priv->flicker = 50;
  359. priv->hmargin = 50;
  360. priv->vmargin = 50;
  361. priv->last_dpms = -1;
  362. priv->chip_version = ch7006_read(client, CH7006_VERSION_ID);
  363. if (ch7006_tv_norm) {
  364. for (i = 0; i < NUM_TV_NORMS; i++) {
  365. if (!strcmp(ch7006_tv_norm_names[i], ch7006_tv_norm)) {
  366. priv->norm = i;
  367. break;
  368. }
  369. }
  370. if (i == NUM_TV_NORMS)
  371. ch7006_err(client, "Invalid TV norm setting \"%s\".\n",
  372. ch7006_tv_norm);
  373. }
  374. if (ch7006_scale >= 0 && ch7006_scale <= 2)
  375. priv->scale = ch7006_scale;
  376. else
  377. ch7006_err(client, "Invalid scale setting \"%d\".\n",
  378. ch7006_scale);
  379. return 0;
  380. }
  381. static struct i2c_device_id ch7006_ids[] = {
  382. { "ch7006", 0 },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(i2c, ch7006_ids);
  386. static struct drm_i2c_encoder_driver ch7006_driver = {
  387. .i2c_driver = {
  388. .probe = ch7006_probe,
  389. .remove = ch7006_remove,
  390. .suspend = ch7006_suspend,
  391. .resume = ch7006_resume,
  392. .driver = {
  393. .name = "ch7006",
  394. },
  395. .id_table = ch7006_ids,
  396. },
  397. .encoder_init = ch7006_encoder_init,
  398. };
  399. /* Module initialization */
  400. static int __init ch7006_init(void)
  401. {
  402. return drm_i2c_encoder_register(THIS_MODULE, &ch7006_driver);
  403. }
  404. static void __exit ch7006_exit(void)
  405. {
  406. drm_i2c_encoder_unregister(&ch7006_driver);
  407. }
  408. int ch7006_debug;
  409. module_param_named(debug, ch7006_debug, int, 0600);
  410. MODULE_PARM_DESC(debug, "Enable debug output.");
  411. char *ch7006_tv_norm;
  412. module_param_named(tv_norm, ch7006_tv_norm, charp, 0600);
  413. MODULE_PARM_DESC(tv_norm, "Default TV norm.\n"
  414. "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, PAL-60, NTSC-M, NTSC-J.\n"
  415. "\t\tDefault: PAL");
  416. int ch7006_scale = 1;
  417. module_param_named(scale, ch7006_scale, int, 0600);
  418. MODULE_PARM_DESC(scale, "Default scale.\n"
  419. "\t\tSupported: 0 -> Select video modes with a higher blanking ratio.\n"
  420. "\t\t\t1 -> Select default video modes.\n"
  421. "\t\t\t2 -> Select video modes with a lower blanking ratio.");
  422. MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
  423. MODULE_DESCRIPTION("Chrontel ch7006 TV encoder driver");
  424. MODULE_LICENSE("GPL and additional rights");
  425. module_init(ch7006_init);
  426. module_exit(ch7006_exit);