cx25840-firmware.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* cx25840 firmware functions
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version 2
  6. * of the License, or (at your option) any later version.
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/firmware.h>
  20. #include <media/v4l2-common.h>
  21. #include <media/cx25840.h>
  22. #include "cx25840-core.h"
  23. #define FWFILE "v4l-cx25840.fw"
  24. #define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw"
  25. /*
  26. * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
  27. * size of the firmware chunks sent down the I2C bus to the chip.
  28. * Previously this had been set to 1024 but unfortunately some I2C
  29. * implementations can't transfer data in such big gulps.
  30. * Specifically, the pvrusb2 driver has a hard limit of around 60
  31. * bytes, due to the encapsulation there of I2C traffic into USB
  32. * messages. So we have to significantly reduce this parameter.
  33. */
  34. #define FWSEND 48
  35. #define FWDEV(x) &((x)->dev)
  36. static char *firmware = FWFILE;
  37. module_param(firmware, charp, 0444);
  38. MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
  39. static void start_fw_load(struct i2c_client *client)
  40. {
  41. /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
  42. cx25840_write(client, 0x800, 0x00);
  43. cx25840_write(client, 0x801, 0x00);
  44. // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
  45. cx25840_write(client, 0x803, 0x0b);
  46. /* AUTO_INC_DIS=1 */
  47. cx25840_write(client, 0x000, 0x20);
  48. }
  49. static void end_fw_load(struct i2c_client *client)
  50. {
  51. /* AUTO_INC_DIS=0 */
  52. cx25840_write(client, 0x000, 0x00);
  53. /* DL_ENABLE=0 */
  54. cx25840_write(client, 0x803, 0x03);
  55. }
  56. static int check_fw_load(struct i2c_client *client, int size)
  57. {
  58. /* DL_ADDR_HB DL_ADDR_LB */
  59. int s = cx25840_read(client, 0x801) << 8;
  60. s |= cx25840_read(client, 0x800);
  61. if (size != s) {
  62. v4l_err(client, "firmware %s load failed\n", firmware);
  63. return -EINVAL;
  64. }
  65. v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
  66. return 0;
  67. }
  68. static int fw_write(struct i2c_client *client, const u8 *data, int size)
  69. {
  70. if (i2c_master_send(client, data, size) < size) {
  71. v4l_err(client, "firmware load i2c failure\n");
  72. return -ENOSYS;
  73. }
  74. return 0;
  75. }
  76. int cx25840_loadfw(struct i2c_client *client)
  77. {
  78. struct cx25840_state *state = i2c_get_clientdata(client);
  79. const struct firmware *fw = NULL;
  80. u8 buffer[FWSEND];
  81. const u8 *ptr;
  82. int size, retval;
  83. if (state->is_cx23885)
  84. firmware = FWFILE_CX23885;
  85. if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
  86. v4l_err(client, "unable to open firmware %s\n", firmware);
  87. return -EINVAL;
  88. }
  89. start_fw_load(client);
  90. buffer[0] = 0x08;
  91. buffer[1] = 0x02;
  92. size = fw->size;
  93. ptr = fw->data;
  94. while (size > 0) {
  95. int len = min(FWSEND - 2, size);
  96. memcpy(buffer + 2, ptr, len);
  97. retval = fw_write(client, buffer, len + 2);
  98. if (retval < 0) {
  99. release_firmware(fw);
  100. return retval;
  101. }
  102. size -= len;
  103. ptr += len;
  104. }
  105. end_fw_load(client);
  106. size = fw->size;
  107. release_firmware(fw);
  108. return check_fw_load(client, size);
  109. }