vpbe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Texas Instruments Inc
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/fs.h>
  10. #include <linux/string.h>
  11. #include <linux/wait.h>
  12. #include <linux/time.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/davinci/vpbe_types.h>
  20. #include <media/davinci/vpbe.h>
  21. #include <media/davinci/vpss.h>
  22. #include <media/davinci/vpbe_venc.h>
  23. #define VPBE_DEFAULT_OUTPUT "Composite"
  24. #define VPBE_DEFAULT_MODE "ntsc"
  25. static char *def_output = VPBE_DEFAULT_OUTPUT;
  26. static char *def_mode = VPBE_DEFAULT_MODE;
  27. static int debug;
  28. module_param(def_output, charp, S_IRUGO);
  29. module_param(def_mode, charp, S_IRUGO);
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(def_output, "vpbe output name (default:Composite)");
  32. MODULE_PARM_DESC(def_mode, "vpbe output mode name (default:ntsc");
  33. MODULE_PARM_DESC(debug, "Debug level 0-1");
  34. MODULE_DESCRIPTION("TI DMXXX VPBE Display controller");
  35. MODULE_LICENSE("GPL");
  36. MODULE_AUTHOR("Texas Instruments");
  37. /**
  38. * vpbe_current_encoder_info - Get config info for current encoder
  39. * @vpbe_dev: vpbe device ptr
  40. *
  41. * Return ptr to current encoder config info
  42. */
  43. static struct encoder_config_info*
  44. vpbe_current_encoder_info(struct vpbe_device *vpbe_dev)
  45. {
  46. struct vpbe_config *cfg = vpbe_dev->cfg;
  47. int index = vpbe_dev->current_sd_index;
  48. return ((index == 0) ? &cfg->venc :
  49. &cfg->ext_encoders[index-1]);
  50. }
  51. /**
  52. * vpbe_find_encoder_sd_index - Given a name find encoder sd index
  53. *
  54. * @cfg: ptr to vpbe cfg
  55. * @index: index used by application
  56. *
  57. * Return sd index of the encoder
  58. */
  59. static int vpbe_find_encoder_sd_index(struct vpbe_config *cfg,
  60. int index)
  61. {
  62. char *encoder_name = cfg->outputs[index].subdev_name;
  63. int i;
  64. /* Venc is always first */
  65. if (!strcmp(encoder_name, cfg->venc.module_name))
  66. return 0;
  67. for (i = 0; i < cfg->num_ext_encoders; i++) {
  68. if (!strcmp(encoder_name,
  69. cfg->ext_encoders[i].module_name))
  70. return i+1;
  71. }
  72. return -EINVAL;
  73. }
  74. /**
  75. * vpbe_enum_outputs - enumerate outputs
  76. * @vpbe_dev: vpbe device ptr
  77. * @output: ptr to v4l2_output structure
  78. *
  79. * Enumerates the outputs available at the vpbe display
  80. * returns the status, -EINVAL if end of output list
  81. */
  82. static int vpbe_enum_outputs(struct vpbe_device *vpbe_dev,
  83. struct v4l2_output *output)
  84. {
  85. struct vpbe_config *cfg = vpbe_dev->cfg;
  86. unsigned int temp_index = output->index;
  87. if (temp_index >= cfg->num_outputs)
  88. return -EINVAL;
  89. *output = cfg->outputs[temp_index].output;
  90. output->index = temp_index;
  91. return 0;
  92. }
  93. static int vpbe_get_mode_info(struct vpbe_device *vpbe_dev, char *mode,
  94. int output_index)
  95. {
  96. struct vpbe_config *cfg = vpbe_dev->cfg;
  97. struct vpbe_enc_mode_info var;
  98. int curr_output = output_index;
  99. int i;
  100. if (!mode)
  101. return -EINVAL;
  102. for (i = 0; i < cfg->outputs[curr_output].num_modes; i++) {
  103. var = cfg->outputs[curr_output].modes[i];
  104. if (!strcmp(mode, var.name)) {
  105. vpbe_dev->current_timings = var;
  106. return 0;
  107. }
  108. }
  109. return -EINVAL;
  110. }
  111. static int vpbe_get_current_mode_info(struct vpbe_device *vpbe_dev,
  112. struct vpbe_enc_mode_info *mode_info)
  113. {
  114. if (!mode_info)
  115. return -EINVAL;
  116. *mode_info = vpbe_dev->current_timings;
  117. return 0;
  118. }
  119. /* Get std by std id */
  120. static int vpbe_get_std_info(struct vpbe_device *vpbe_dev,
  121. v4l2_std_id std_id)
  122. {
  123. struct vpbe_config *cfg = vpbe_dev->cfg;
  124. struct vpbe_enc_mode_info var;
  125. int curr_output = vpbe_dev->current_out_index;
  126. int i;
  127. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  128. var = cfg->outputs[curr_output].modes[i];
  129. if ((var.timings_type & VPBE_ENC_STD) &&
  130. (var.std_id & std_id)) {
  131. vpbe_dev->current_timings = var;
  132. return 0;
  133. }
  134. }
  135. return -EINVAL;
  136. }
  137. static int vpbe_get_std_info_by_name(struct vpbe_device *vpbe_dev,
  138. char *std_name)
  139. {
  140. struct vpbe_config *cfg = vpbe_dev->cfg;
  141. struct vpbe_enc_mode_info var;
  142. int curr_output = vpbe_dev->current_out_index;
  143. int i;
  144. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  145. var = cfg->outputs[curr_output].modes[i];
  146. if (!strcmp(var.name, std_name)) {
  147. vpbe_dev->current_timings = var;
  148. return 0;
  149. }
  150. }
  151. return -EINVAL;
  152. }
  153. /**
  154. * vpbe_set_output - Set output
  155. * @vpbe_dev: vpbe device ptr
  156. * @index: index of output
  157. *
  158. * Set vpbe output to the output specified by the index
  159. */
  160. static int vpbe_set_output(struct vpbe_device *vpbe_dev, int index)
  161. {
  162. struct encoder_config_info *curr_enc_info =
  163. vpbe_current_encoder_info(vpbe_dev);
  164. struct vpbe_config *cfg = vpbe_dev->cfg;
  165. struct venc_platform_data *venc_device = vpbe_dev->venc_device;
  166. int enc_out_index;
  167. int sd_index;
  168. int ret;
  169. if (index >= cfg->num_outputs)
  170. return -EINVAL;
  171. mutex_lock(&vpbe_dev->lock);
  172. sd_index = vpbe_dev->current_sd_index;
  173. enc_out_index = cfg->outputs[index].output.index;
  174. /*
  175. * Currently we switch the encoder based on output selected
  176. * by the application. If media controller is implemented later
  177. * there is will be an API added to setup_link between venc
  178. * and external encoder. So in that case below comparison always
  179. * match and encoder will not be switched. But if application
  180. * chose not to use media controller, then this provides current
  181. * way of switching encoder at the venc output.
  182. */
  183. if (strcmp(curr_enc_info->module_name,
  184. cfg->outputs[index].subdev_name)) {
  185. /* Need to switch the encoder at the output */
  186. sd_index = vpbe_find_encoder_sd_index(cfg, index);
  187. if (sd_index < 0) {
  188. ret = -EINVAL;
  189. goto unlock;
  190. }
  191. ret = venc_device->setup_if_config(cfg->outputs[index].if_params);
  192. if (ret)
  193. goto unlock;
  194. }
  195. /* Set output at the encoder */
  196. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  197. s_routing, 0, enc_out_index, 0);
  198. if (ret)
  199. goto unlock;
  200. /*
  201. * It is assumed that venc or external encoder will set a default
  202. * mode in the sub device. For external encoder or LCD pannel output,
  203. * we also need to set up the lcd port for the required mode. So setup
  204. * the lcd port for the default mode that is configured in the board
  205. * arch/arm/mach-davinci/board-dm355-evm.setup file for the external
  206. * encoder.
  207. */
  208. ret = vpbe_get_mode_info(vpbe_dev,
  209. cfg->outputs[index].default_mode, index);
  210. if (!ret) {
  211. struct osd_state *osd_device = vpbe_dev->osd_device;
  212. osd_device->ops.set_left_margin(osd_device,
  213. vpbe_dev->current_timings.left_margin);
  214. osd_device->ops.set_top_margin(osd_device,
  215. vpbe_dev->current_timings.upper_margin);
  216. vpbe_dev->current_sd_index = sd_index;
  217. vpbe_dev->current_out_index = index;
  218. }
  219. unlock:
  220. mutex_unlock(&vpbe_dev->lock);
  221. return ret;
  222. }
  223. static int vpbe_set_default_output(struct vpbe_device *vpbe_dev)
  224. {
  225. struct vpbe_config *cfg = vpbe_dev->cfg;
  226. int i;
  227. for (i = 0; i < cfg->num_outputs; i++) {
  228. if (!strcmp(def_output,
  229. cfg->outputs[i].output.name)) {
  230. int ret = vpbe_set_output(vpbe_dev, i);
  231. if (!ret)
  232. vpbe_dev->current_out_index = i;
  233. return ret;
  234. }
  235. }
  236. return 0;
  237. }
  238. /**
  239. * vpbe_get_output - Get output
  240. * @vpbe_dev: vpbe device ptr
  241. *
  242. * return current vpbe output to the the index
  243. */
  244. static unsigned int vpbe_get_output(struct vpbe_device *vpbe_dev)
  245. {
  246. return vpbe_dev->current_out_index;
  247. }
  248. /*
  249. * vpbe_s_dv_timings - Set the given preset timings in the encoder
  250. *
  251. * Sets the timings if supported by the current encoder. Return the status.
  252. * 0 - success & -EINVAL on error
  253. */
  254. static int vpbe_s_dv_timings(struct vpbe_device *vpbe_dev,
  255. struct v4l2_dv_timings *dv_timings)
  256. {
  257. struct vpbe_config *cfg = vpbe_dev->cfg;
  258. int out_index = vpbe_dev->current_out_index;
  259. struct vpbe_output *output = &cfg->outputs[out_index];
  260. int sd_index = vpbe_dev->current_sd_index;
  261. int ret, i;
  262. if (!(cfg->outputs[out_index].output.capabilities &
  263. V4L2_OUT_CAP_DV_TIMINGS))
  264. return -ENODATA;
  265. for (i = 0; i < output->num_modes; i++) {
  266. if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS &&
  267. !memcmp(&output->modes[i].dv_timings,
  268. dv_timings, sizeof(*dv_timings)))
  269. break;
  270. }
  271. if (i >= output->num_modes)
  272. return -EINVAL;
  273. vpbe_dev->current_timings = output->modes[i];
  274. mutex_lock(&vpbe_dev->lock);
  275. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  276. s_dv_timings, dv_timings);
  277. if (!ret && vpbe_dev->amp) {
  278. /* Call amplifier subdevice */
  279. ret = v4l2_subdev_call(vpbe_dev->amp, video,
  280. s_dv_timings, dv_timings);
  281. }
  282. /* set the lcd controller output for the given mode */
  283. if (!ret) {
  284. struct osd_state *osd_device = vpbe_dev->osd_device;
  285. osd_device->ops.set_left_margin(osd_device,
  286. vpbe_dev->current_timings.left_margin);
  287. osd_device->ops.set_top_margin(osd_device,
  288. vpbe_dev->current_timings.upper_margin);
  289. }
  290. mutex_unlock(&vpbe_dev->lock);
  291. return ret;
  292. }
  293. /*
  294. * vpbe_g_dv_timings - Get the timings in the current encoder
  295. *
  296. * Get the timings in the current encoder. Return the status. 0 - success
  297. * -EINVAL on error
  298. */
  299. static int vpbe_g_dv_timings(struct vpbe_device *vpbe_dev,
  300. struct v4l2_dv_timings *dv_timings)
  301. {
  302. struct vpbe_config *cfg = vpbe_dev->cfg;
  303. int out_index = vpbe_dev->current_out_index;
  304. if (!(cfg->outputs[out_index].output.capabilities &
  305. V4L2_OUT_CAP_DV_TIMINGS))
  306. return -ENODATA;
  307. if (vpbe_dev->current_timings.timings_type &
  308. VPBE_ENC_DV_TIMINGS) {
  309. *dv_timings = vpbe_dev->current_timings.dv_timings;
  310. return 0;
  311. }
  312. return -EINVAL;
  313. }
  314. /*
  315. * vpbe_enum_dv_timings - Enumerate the dv timings in the current encoder
  316. *
  317. * Get the timings in the current encoder. Return the status. 0 - success
  318. * -EINVAL on error
  319. */
  320. static int vpbe_enum_dv_timings(struct vpbe_device *vpbe_dev,
  321. struct v4l2_enum_dv_timings *timings)
  322. {
  323. struct vpbe_config *cfg = vpbe_dev->cfg;
  324. int out_index = vpbe_dev->current_out_index;
  325. struct vpbe_output *output = &cfg->outputs[out_index];
  326. int j = 0;
  327. int i;
  328. if (!(output->output.capabilities & V4L2_OUT_CAP_DV_TIMINGS))
  329. return -ENODATA;
  330. for (i = 0; i < output->num_modes; i++) {
  331. if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS) {
  332. if (j == timings->index)
  333. break;
  334. j++;
  335. }
  336. }
  337. if (i == output->num_modes)
  338. return -EINVAL;
  339. timings->timings = output->modes[i].dv_timings;
  340. return 0;
  341. }
  342. /*
  343. * vpbe_s_std - Set the given standard in the encoder
  344. *
  345. * Sets the standard if supported by the current encoder. Return the status.
  346. * 0 - success & -EINVAL on error
  347. */
  348. static int vpbe_s_std(struct vpbe_device *vpbe_dev, v4l2_std_id std_id)
  349. {
  350. struct vpbe_config *cfg = vpbe_dev->cfg;
  351. int out_index = vpbe_dev->current_out_index;
  352. int sd_index = vpbe_dev->current_sd_index;
  353. int ret;
  354. if (!(cfg->outputs[out_index].output.capabilities &
  355. V4L2_OUT_CAP_STD))
  356. return -ENODATA;
  357. ret = vpbe_get_std_info(vpbe_dev, std_id);
  358. if (ret)
  359. return ret;
  360. mutex_lock(&vpbe_dev->lock);
  361. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  362. s_std_output, std_id);
  363. /* set the lcd controller output for the given mode */
  364. if (!ret) {
  365. struct osd_state *osd_device = vpbe_dev->osd_device;
  366. osd_device->ops.set_left_margin(osd_device,
  367. vpbe_dev->current_timings.left_margin);
  368. osd_device->ops.set_top_margin(osd_device,
  369. vpbe_dev->current_timings.upper_margin);
  370. }
  371. mutex_unlock(&vpbe_dev->lock);
  372. return ret;
  373. }
  374. /*
  375. * vpbe_g_std - Get the standard in the current encoder
  376. *
  377. * Get the standard in the current encoder. Return the status. 0 - success
  378. * -EINVAL on error
  379. */
  380. static int vpbe_g_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
  381. {
  382. struct vpbe_enc_mode_info *cur_timings = &vpbe_dev->current_timings;
  383. struct vpbe_config *cfg = vpbe_dev->cfg;
  384. int out_index = vpbe_dev->current_out_index;
  385. if (!(cfg->outputs[out_index].output.capabilities & V4L2_OUT_CAP_STD))
  386. return -ENODATA;
  387. if (cur_timings->timings_type & VPBE_ENC_STD) {
  388. *std_id = cur_timings->std_id;
  389. return 0;
  390. }
  391. return -EINVAL;
  392. }
  393. /*
  394. * vpbe_set_mode - Set mode in the current encoder using mode info
  395. *
  396. * Use the mode string to decide what timings to set in the encoder
  397. * This is typically useful when fbset command is used to change the current
  398. * timings by specifying a string to indicate the timings.
  399. */
  400. static int vpbe_set_mode(struct vpbe_device *vpbe_dev,
  401. struct vpbe_enc_mode_info *mode_info)
  402. {
  403. struct vpbe_enc_mode_info *preset_mode = NULL;
  404. struct vpbe_config *cfg = vpbe_dev->cfg;
  405. struct v4l2_dv_timings dv_timings;
  406. struct osd_state *osd_device;
  407. int out_index = vpbe_dev->current_out_index;
  408. int i;
  409. if (!mode_info || !mode_info->name)
  410. return -EINVAL;
  411. for (i = 0; i < cfg->outputs[out_index].num_modes; i++) {
  412. if (!strcmp(mode_info->name,
  413. cfg->outputs[out_index].modes[i].name)) {
  414. preset_mode = &cfg->outputs[out_index].modes[i];
  415. /*
  416. * it may be one of the 3 timings type. Check and
  417. * invoke right API
  418. */
  419. if (preset_mode->timings_type & VPBE_ENC_STD)
  420. return vpbe_s_std(vpbe_dev,
  421. preset_mode->std_id);
  422. if (preset_mode->timings_type &
  423. VPBE_ENC_DV_TIMINGS) {
  424. dv_timings =
  425. preset_mode->dv_timings;
  426. return vpbe_s_dv_timings(vpbe_dev, &dv_timings);
  427. }
  428. }
  429. }
  430. /* Only custom timing should reach here */
  431. if (!preset_mode)
  432. return -EINVAL;
  433. mutex_lock(&vpbe_dev->lock);
  434. osd_device = vpbe_dev->osd_device;
  435. vpbe_dev->current_timings = *preset_mode;
  436. osd_device->ops.set_left_margin(osd_device,
  437. vpbe_dev->current_timings.left_margin);
  438. osd_device->ops.set_top_margin(osd_device,
  439. vpbe_dev->current_timings.upper_margin);
  440. mutex_unlock(&vpbe_dev->lock);
  441. return 0;
  442. }
  443. static int vpbe_set_default_mode(struct vpbe_device *vpbe_dev)
  444. {
  445. int ret;
  446. ret = vpbe_get_std_info_by_name(vpbe_dev, def_mode);
  447. if (ret)
  448. return ret;
  449. /* set the default mode in the encoder */
  450. return vpbe_set_mode(vpbe_dev, &vpbe_dev->current_timings);
  451. }
  452. static int platform_device_get(struct device *dev, void *data)
  453. {
  454. struct platform_device *pdev = to_platform_device(dev);
  455. struct vpbe_device *vpbe_dev = data;
  456. if (strstr(pdev->name, "vpbe-osd"))
  457. vpbe_dev->osd_device = platform_get_drvdata(pdev);
  458. if (strstr(pdev->name, "vpbe-venc"))
  459. vpbe_dev->venc_device = dev_get_platdata(&pdev->dev);
  460. return 0;
  461. }
  462. /**
  463. * vpbe_initialize() - Initialize the vpbe display controller
  464. * @dev: Master and slave device ptr
  465. * @vpbe_dev: vpbe device ptr
  466. *
  467. * Master frame buffer device drivers calls this to initialize vpbe
  468. * display controller. This will then registers v4l2 device and the sub
  469. * devices and sets a current encoder sub device for display. v4l2 display
  470. * device driver is the master and frame buffer display device driver is
  471. * the slave. Frame buffer display driver checks the initialized during
  472. * probe and exit if not initialized. Returns status.
  473. */
  474. static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
  475. {
  476. struct encoder_config_info *enc_info;
  477. struct amp_config_info *amp_info;
  478. struct v4l2_subdev **enc_subdev;
  479. struct osd_state *osd_device;
  480. struct i2c_adapter *i2c_adap;
  481. int num_encoders;
  482. int ret = 0;
  483. int err;
  484. int i;
  485. /*
  486. * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
  487. * from the platform device by iteration of platform drivers and
  488. * matching with device name
  489. */
  490. if (!vpbe_dev || !dev) {
  491. printk(KERN_ERR "Null device pointers.\n");
  492. return -ENODEV;
  493. }
  494. if (vpbe_dev->initialized)
  495. return 0;
  496. mutex_lock(&vpbe_dev->lock);
  497. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  498. /* We have dac clock available for platform */
  499. vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
  500. if (IS_ERR(vpbe_dev->dac_clk)) {
  501. ret = PTR_ERR(vpbe_dev->dac_clk);
  502. goto fail_mutex_unlock;
  503. }
  504. if (clk_prepare_enable(vpbe_dev->dac_clk)) {
  505. ret = -ENODEV;
  506. clk_put(vpbe_dev->dac_clk);
  507. goto fail_mutex_unlock;
  508. }
  509. }
  510. /* first enable vpss clocks */
  511. vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
  512. /* First register a v4l2 device */
  513. ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
  514. if (ret) {
  515. v4l2_err(dev->driver,
  516. "Unable to register v4l2 device.\n");
  517. goto fail_clk_put;
  518. }
  519. v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
  520. err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
  521. platform_device_get);
  522. if (err < 0) {
  523. ret = err;
  524. goto fail_dev_unregister;
  525. }
  526. vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
  527. vpbe_dev->cfg->venc.module_name);
  528. /* register venc sub device */
  529. if (!vpbe_dev->venc) {
  530. v4l2_err(&vpbe_dev->v4l2_dev,
  531. "vpbe unable to init venc sub device\n");
  532. ret = -ENODEV;
  533. goto fail_dev_unregister;
  534. }
  535. /* initialize osd device */
  536. osd_device = vpbe_dev->osd_device;
  537. if (osd_device->ops.initialize) {
  538. err = osd_device->ops.initialize(osd_device);
  539. if (err) {
  540. v4l2_err(&vpbe_dev->v4l2_dev,
  541. "unable to initialize the OSD device");
  542. err = -ENOMEM;
  543. goto fail_dev_unregister;
  544. }
  545. }
  546. /*
  547. * Register any external encoders that are configured. At index 0 we
  548. * store venc sd index.
  549. */
  550. num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
  551. vpbe_dev->encoders = kmalloc_array(num_encoders,
  552. sizeof(*vpbe_dev->encoders),
  553. GFP_KERNEL);
  554. if (!vpbe_dev->encoders) {
  555. ret = -ENOMEM;
  556. goto fail_dev_unregister;
  557. }
  558. i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
  559. for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
  560. if (i == 0) {
  561. /* venc is at index 0 */
  562. enc_subdev = &vpbe_dev->encoders[i];
  563. *enc_subdev = vpbe_dev->venc;
  564. continue;
  565. }
  566. enc_info = &vpbe_dev->cfg->ext_encoders[i];
  567. if (enc_info->is_i2c) {
  568. enc_subdev = &vpbe_dev->encoders[i];
  569. *enc_subdev = v4l2_i2c_new_subdev_board(
  570. &vpbe_dev->v4l2_dev, i2c_adap,
  571. &enc_info->board_info, NULL);
  572. if (*enc_subdev)
  573. v4l2_info(&vpbe_dev->v4l2_dev,
  574. "v4l2 sub device %s registered\n",
  575. enc_info->module_name);
  576. else {
  577. v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s failed to register",
  578. enc_info->module_name);
  579. ret = -ENODEV;
  580. goto fail_kfree_encoders;
  581. }
  582. } else
  583. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders currently not supported");
  584. }
  585. /* Add amplifier subdevice for dm365 */
  586. if ((strcmp(vpbe_dev->cfg->module_name, "dm365-vpbe-display") == 0) &&
  587. vpbe_dev->cfg->amp) {
  588. amp_info = vpbe_dev->cfg->amp;
  589. if (amp_info->is_i2c) {
  590. vpbe_dev->amp = v4l2_i2c_new_subdev_board(
  591. &vpbe_dev->v4l2_dev, i2c_adap,
  592. &amp_info->board_info, NULL);
  593. if (!vpbe_dev->amp) {
  594. v4l2_err(&vpbe_dev->v4l2_dev,
  595. "amplifier %s failed to register",
  596. amp_info->module_name);
  597. ret = -ENODEV;
  598. goto fail_kfree_encoders;
  599. }
  600. v4l2_info(&vpbe_dev->v4l2_dev,
  601. "v4l2 sub device %s registered\n",
  602. amp_info->module_name);
  603. } else {
  604. vpbe_dev->amp = NULL;
  605. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c amplifiers currently not supported");
  606. }
  607. } else {
  608. vpbe_dev->amp = NULL;
  609. }
  610. /* set the current encoder and output to that of venc by default */
  611. vpbe_dev->current_sd_index = 0;
  612. vpbe_dev->current_out_index = 0;
  613. mutex_unlock(&vpbe_dev->lock);
  614. printk(KERN_NOTICE "Setting default output to %s\n", def_output);
  615. ret = vpbe_set_default_output(vpbe_dev);
  616. if (ret) {
  617. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
  618. def_output);
  619. goto fail_kfree_amp;
  620. }
  621. printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
  622. ret = vpbe_set_default_mode(vpbe_dev);
  623. if (ret) {
  624. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
  625. def_mode);
  626. goto fail_kfree_amp;
  627. }
  628. vpbe_dev->initialized = 1;
  629. /* TBD handling of bootargs for default output and mode */
  630. return 0;
  631. fail_kfree_amp:
  632. mutex_lock(&vpbe_dev->lock);
  633. kfree(vpbe_dev->amp);
  634. fail_kfree_encoders:
  635. kfree(vpbe_dev->encoders);
  636. fail_dev_unregister:
  637. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  638. fail_clk_put:
  639. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  640. clk_disable_unprepare(vpbe_dev->dac_clk);
  641. clk_put(vpbe_dev->dac_clk);
  642. }
  643. fail_mutex_unlock:
  644. mutex_unlock(&vpbe_dev->lock);
  645. return ret;
  646. }
  647. /**
  648. * vpbe_deinitialize() - de-initialize the vpbe display controller
  649. * @dev: Master and slave device ptr
  650. * @vpbe_dev: vpbe device ptr
  651. *
  652. * vpbe_master and slave frame buffer devices calls this to de-initialize
  653. * the display controller. It is called when master and slave device
  654. * driver modules are removed and no longer requires the display controller.
  655. */
  656. static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
  657. {
  658. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  659. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  660. clk_disable_unprepare(vpbe_dev->dac_clk);
  661. clk_put(vpbe_dev->dac_clk);
  662. }
  663. kfree(vpbe_dev->amp);
  664. kfree(vpbe_dev->encoders);
  665. vpbe_dev->initialized = 0;
  666. /* disable vpss clocks */
  667. vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
  668. }
  669. static const struct vpbe_device_ops vpbe_dev_ops = {
  670. .enum_outputs = vpbe_enum_outputs,
  671. .set_output = vpbe_set_output,
  672. .get_output = vpbe_get_output,
  673. .s_dv_timings = vpbe_s_dv_timings,
  674. .g_dv_timings = vpbe_g_dv_timings,
  675. .enum_dv_timings = vpbe_enum_dv_timings,
  676. .s_std = vpbe_s_std,
  677. .g_std = vpbe_g_std,
  678. .initialize = vpbe_initialize,
  679. .deinitialize = vpbe_deinitialize,
  680. .get_mode_info = vpbe_get_current_mode_info,
  681. .set_mode = vpbe_set_mode,
  682. };
  683. static int vpbe_probe(struct platform_device *pdev)
  684. {
  685. struct vpbe_device *vpbe_dev;
  686. struct vpbe_config *cfg;
  687. if (!pdev->dev.platform_data) {
  688. v4l2_err(pdev->dev.driver, "No platform data\n");
  689. return -ENODEV;
  690. }
  691. cfg = pdev->dev.platform_data;
  692. if (!cfg->module_name[0] ||
  693. !cfg->osd.module_name[0] ||
  694. !cfg->venc.module_name[0]) {
  695. v4l2_err(pdev->dev.driver, "vpbe display module names not defined\n");
  696. return -EINVAL;
  697. }
  698. vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
  699. if (!vpbe_dev)
  700. return -ENOMEM;
  701. vpbe_dev->cfg = cfg;
  702. vpbe_dev->ops = vpbe_dev_ops;
  703. vpbe_dev->pdev = &pdev->dev;
  704. if (cfg->outputs->num_modes > 0)
  705. vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
  706. else {
  707. kfree(vpbe_dev);
  708. return -ENODEV;
  709. }
  710. /* set the driver data in platform device */
  711. platform_set_drvdata(pdev, vpbe_dev);
  712. mutex_init(&vpbe_dev->lock);
  713. return 0;
  714. }
  715. static int vpbe_remove(struct platform_device *device)
  716. {
  717. struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
  718. kfree(vpbe_dev);
  719. return 0;
  720. }
  721. static struct platform_driver vpbe_driver = {
  722. .driver = {
  723. .name = "vpbe_controller",
  724. },
  725. .probe = vpbe_probe,
  726. .remove = vpbe_remove,
  727. };
  728. module_platform_driver(vpbe_driver);