ch7006_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. if (!priv->scale_property)
  201. return -ENOMEM;
  202. drm_object_attach_property(&connector->base, conf->tv_select_subconnector_property,
  203. priv->select_subconnector);
  204. drm_object_attach_property(&connector->base, conf->tv_subconnector_property,
  205. priv->subconnector);
  206. drm_object_attach_property(&connector->base, conf->tv_left_margin_property,
  207. priv->hmargin);
  208. drm_object_attach_property(&connector->base, conf->tv_bottom_margin_property,
  209. priv->vmargin);
  210. drm_object_attach_property(&connector->base, conf->tv_mode_property,
  211. priv->norm);
  212. drm_object_attach_property(&connector->base, conf->tv_brightness_property,
  213. priv->brightness);
  214. drm_object_attach_property(&connector->base, conf->tv_contrast_property,
  215. priv->contrast);
  216. drm_object_attach_property(&connector->base, conf->tv_flicker_reduction_property,
  217. priv->flicker);
  218. drm_object_attach_property(&connector->base, 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. drm_crtc_force_disable(crtc);
  279. }
  280. return 0;
  281. }
  282. static const struct drm_encoder_slave_funcs ch7006_encoder_funcs = {
  283. .set_config = ch7006_encoder_set_config,
  284. .destroy = ch7006_encoder_destroy,
  285. .dpms = ch7006_encoder_dpms,
  286. .save = ch7006_encoder_save,
  287. .restore = ch7006_encoder_restore,
  288. .mode_fixup = ch7006_encoder_mode_fixup,
  289. .mode_valid = ch7006_encoder_mode_valid,
  290. .mode_set = ch7006_encoder_mode_set,
  291. .detect = ch7006_encoder_detect,
  292. .get_modes = ch7006_encoder_get_modes,
  293. .create_resources = ch7006_encoder_create_resources,
  294. .set_property = ch7006_encoder_set_property,
  295. };
  296. /* I2C driver functions */
  297. static int ch7006_probe(struct i2c_client *client, const struct i2c_device_id *id)
  298. {
  299. uint8_t addr = CH7006_VERSION_ID;
  300. uint8_t val;
  301. int ret;
  302. ch7006_dbg(client, "\n");
  303. ret = i2c_master_send(client, &addr, sizeof(addr));
  304. if (ret < 0)
  305. goto fail;
  306. ret = i2c_master_recv(client, &val, sizeof(val));
  307. if (ret < 0)
  308. goto fail;
  309. ch7006_info(client, "Detected version ID: %x\n", val);
  310. /* I don't know what this is for, but otherwise I get no
  311. * signal.
  312. */
  313. ch7006_write(client, 0x3d, 0x0);
  314. return 0;
  315. fail:
  316. ch7006_err(client, "Error %d reading version ID\n", ret);
  317. return -ENODEV;
  318. }
  319. static int ch7006_remove(struct i2c_client *client)
  320. {
  321. ch7006_dbg(client, "\n");
  322. return 0;
  323. }
  324. static int ch7006_resume(struct device *dev)
  325. {
  326. struct i2c_client *client = to_i2c_client(dev);
  327. ch7006_dbg(client, "\n");
  328. ch7006_write(client, 0x3d, 0x0);
  329. return 0;
  330. }
  331. static int ch7006_encoder_init(struct i2c_client *client,
  332. struct drm_device *dev,
  333. struct drm_encoder_slave *encoder)
  334. {
  335. struct ch7006_priv *priv;
  336. int i;
  337. ch7006_dbg(client, "\n");
  338. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  339. if (!priv)
  340. return -ENOMEM;
  341. encoder->slave_priv = priv;
  342. encoder->slave_funcs = &ch7006_encoder_funcs;
  343. priv->norm = TV_NORM_PAL;
  344. priv->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
  345. priv->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  346. priv->scale = 1;
  347. priv->contrast = 50;
  348. priv->brightness = 50;
  349. priv->flicker = 50;
  350. priv->hmargin = 50;
  351. priv->vmargin = 50;
  352. priv->last_dpms = -1;
  353. priv->chip_version = ch7006_read(client, CH7006_VERSION_ID);
  354. if (ch7006_tv_norm) {
  355. for (i = 0; i < NUM_TV_NORMS; i++) {
  356. if (!strcmp(ch7006_tv_norm_names[i], ch7006_tv_norm)) {
  357. priv->norm = i;
  358. break;
  359. }
  360. }
  361. if (i == NUM_TV_NORMS)
  362. ch7006_err(client, "Invalid TV norm setting \"%s\".\n",
  363. ch7006_tv_norm);
  364. }
  365. if (ch7006_scale >= 0 && ch7006_scale <= 2)
  366. priv->scale = ch7006_scale;
  367. else
  368. ch7006_err(client, "Invalid scale setting \"%d\".\n",
  369. ch7006_scale);
  370. return 0;
  371. }
  372. static const struct i2c_device_id ch7006_ids[] = {
  373. { "ch7006", 0 },
  374. { }
  375. };
  376. MODULE_DEVICE_TABLE(i2c, ch7006_ids);
  377. static const struct dev_pm_ops ch7006_pm_ops = {
  378. .resume = ch7006_resume,
  379. };
  380. static struct drm_i2c_encoder_driver ch7006_driver = {
  381. .i2c_driver = {
  382. .probe = ch7006_probe,
  383. .remove = ch7006_remove,
  384. .driver = {
  385. .name = "ch7006",
  386. .pm = &ch7006_pm_ops,
  387. },
  388. .id_table = ch7006_ids,
  389. },
  390. .encoder_init = ch7006_encoder_init,
  391. };
  392. /* Module initialization */
  393. static int __init ch7006_init(void)
  394. {
  395. return drm_i2c_encoder_register(THIS_MODULE, &ch7006_driver);
  396. }
  397. static void __exit ch7006_exit(void)
  398. {
  399. drm_i2c_encoder_unregister(&ch7006_driver);
  400. }
  401. int ch7006_debug;
  402. module_param_named(debug, ch7006_debug, int, 0600);
  403. MODULE_PARM_DESC(debug, "Enable debug output.");
  404. char *ch7006_tv_norm;
  405. module_param_named(tv_norm, ch7006_tv_norm, charp, 0600);
  406. MODULE_PARM_DESC(tv_norm, "Default TV norm.\n"
  407. "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, PAL-60, NTSC-M, NTSC-J.\n"
  408. "\t\tDefault: PAL");
  409. int ch7006_scale = 1;
  410. module_param_named(scale, ch7006_scale, int, 0600);
  411. MODULE_PARM_DESC(scale, "Default scale.\n"
  412. "\t\tSupported: 0 -> Select video modes with a higher blanking ratio.\n"
  413. "\t\t\t1 -> Select default video modes.\n"
  414. "\t\t\t2 -> Select video modes with a lower blanking ratio.");
  415. MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
  416. MODULE_DESCRIPTION("Chrontel ch7006 TV encoder driver");
  417. MODULE_LICENSE("GPL and additional rights");
  418. module_init(ch7006_init);
  419. module_exit(ch7006_exit);