vpbe.c 23 KB

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