cx25840-firmware.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/i2c-algo-bit.h>
  20. #include <linux/firmware.h>
  21. #include <media/v4l2-common.h>
  22. #include <media/cx25840.h>
  23. #include "cx25840-core.h"
  24. #define FWFILE "v4l-cx25840.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)->adapter->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, u8 * data, int size)
  69. {
  70. int sent;
  71. if ((sent = i2c_master_send(client, data, size)) < size) {
  72. v4l_err(client, "firmware load i2c failure\n");
  73. return -ENOSYS;
  74. }
  75. return 0;
  76. }
  77. int cx25840_loadfw(struct i2c_client *client)
  78. {
  79. const struct firmware *fw = NULL;
  80. u8 buffer[4], *ptr;
  81. int size, send, retval;
  82. if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
  83. v4l_err(client, "unable to open firmware %s\n", firmware);
  84. return -EINVAL;
  85. }
  86. start_fw_load(client);
  87. buffer[0] = 0x08;
  88. buffer[1] = 0x02;
  89. buffer[2] = fw->data[0];
  90. buffer[3] = fw->data[1];
  91. retval = fw_write(client, buffer, 4);
  92. if (retval < 0) {
  93. release_firmware(fw);
  94. return retval;
  95. }
  96. size = fw->size - 2;
  97. ptr = fw->data;
  98. while (size > 0) {
  99. ptr[0] = 0x08;
  100. ptr[1] = 0x02;
  101. send = size > (FWSEND - 2) ? FWSEND : size + 2;
  102. retval = fw_write(client, ptr, send);
  103. if (retval < 0) {
  104. release_firmware(fw);
  105. return retval;
  106. }
  107. size -= FWSEND - 2;
  108. ptr += FWSEND - 2;
  109. }
  110. end_fw_load(client);
  111. size = fw->size;
  112. release_firmware(fw);
  113. return check_fw_load(client, size);
  114. }