au8522_common.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Auvitek AU8522 QAM/8VSB demodulator driver
  3. Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
  4. Copyright (C) 2008 Devin Heitmueller <dheitmueller@linuxtv.org>
  5. Copyright (C) 2005-2008 Auvitek International, Ltd.
  6. Copyright (C) 2012 Michael Krufky <mkrufky@linuxtv.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/i2c.h>
  20. #include "dvb_frontend.h"
  21. #include "au8522_priv.h"
  22. static int debug;
  23. #define dprintk(arg...)\
  24. do { if (debug)\
  25. printk(arg);\
  26. } while (0)
  27. /* Despite the name "hybrid_tuner", the framework works just as well for
  28. hybrid demodulators as well... */
  29. static LIST_HEAD(hybrid_tuner_instance_list);
  30. static DEFINE_MUTEX(au8522_list_mutex);
  31. /* 16 bit registers, 8 bit values */
  32. int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
  33. {
  34. int ret;
  35. u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
  36. struct i2c_msg msg = { .addr = state->config->demod_address,
  37. .flags = 0, .buf = buf, .len = 3 };
  38. ret = i2c_transfer(state->i2c, &msg, 1);
  39. if (ret != 1)
  40. printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
  41. "ret == %i)\n", __func__, reg, data, ret);
  42. return (ret != 1) ? -1 : 0;
  43. }
  44. EXPORT_SYMBOL(au8522_writereg);
  45. u8 au8522_readreg(struct au8522_state *state, u16 reg)
  46. {
  47. int ret;
  48. u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff };
  49. u8 b1[] = { 0 };
  50. struct i2c_msg msg[] = {
  51. { .addr = state->config->demod_address, .flags = 0,
  52. .buf = b0, .len = 2 },
  53. { .addr = state->config->demod_address, .flags = I2C_M_RD,
  54. .buf = b1, .len = 1 } };
  55. ret = i2c_transfer(state->i2c, msg, 2);
  56. if (ret != 2)
  57. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  58. __func__, ret);
  59. return b1[0];
  60. }
  61. EXPORT_SYMBOL(au8522_readreg);
  62. int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  63. {
  64. struct au8522_state *state = fe->demodulator_priv;
  65. dprintk("%s(%d)\n", __func__, enable);
  66. if (state->operational_mode == AU8522_ANALOG_MODE) {
  67. /* We're being asked to manage the gate even though we're
  68. not in digital mode. This can occur if we get switched
  69. over to analog mode before the dvb_frontend kernel thread
  70. has completely shutdown */
  71. return 0;
  72. }
  73. if (enable)
  74. return au8522_writereg(state, 0x106, 1);
  75. else
  76. return au8522_writereg(state, 0x106, 0);
  77. }
  78. EXPORT_SYMBOL(au8522_i2c_gate_ctrl);
  79. int au8522_analog_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  80. {
  81. struct au8522_state *state = fe->demodulator_priv;
  82. dprintk("%s(%d)\n", __func__, enable);
  83. if (enable)
  84. return au8522_writereg(state, 0x106, 1);
  85. else
  86. return au8522_writereg(state, 0x106, 0);
  87. }
  88. EXPORT_SYMBOL(au8522_analog_i2c_gate_ctrl);
  89. /* Reset the demod hardware and reset all of the configuration registers
  90. to a default state. */
  91. int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
  92. u8 client_address)
  93. {
  94. int ret;
  95. mutex_lock(&au8522_list_mutex);
  96. ret = hybrid_tuner_request_state(struct au8522_state, (*state),
  97. hybrid_tuner_instance_list,
  98. i2c, client_address, "au8522");
  99. mutex_unlock(&au8522_list_mutex);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(au8522_get_state);
  103. void au8522_release_state(struct au8522_state *state)
  104. {
  105. mutex_lock(&au8522_list_mutex);
  106. if (state != NULL)
  107. hybrid_tuner_release_state(state);
  108. mutex_unlock(&au8522_list_mutex);
  109. }
  110. EXPORT_SYMBOL(au8522_release_state);
  111. static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
  112. {
  113. struct au8522_led_config *led_config = state->config->led_cfg;
  114. u8 val;
  115. /* bail out if we can't control an LED */
  116. if (!led_config || !led_config->gpio_output ||
  117. !led_config->gpio_output_enable || !led_config->gpio_output_disable)
  118. return 0;
  119. val = au8522_readreg(state, 0x4000 |
  120. (led_config->gpio_output & ~0xc000));
  121. if (onoff) {
  122. /* enable GPIO output */
  123. val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
  124. val |= (led_config->gpio_output_enable & 0xff);
  125. } else {
  126. /* disable GPIO output */
  127. val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
  128. val |= (led_config->gpio_output_disable & 0xff);
  129. }
  130. return au8522_writereg(state, 0x8000 |
  131. (led_config->gpio_output & ~0xc000), val);
  132. }
  133. /* led = 0 | off
  134. * led = 1 | signal ok
  135. * led = 2 | signal strong
  136. * led < 0 | only light led if leds are currently off
  137. */
  138. int au8522_led_ctrl(struct au8522_state *state, int led)
  139. {
  140. struct au8522_led_config *led_config = state->config->led_cfg;
  141. int i, ret = 0;
  142. /* bail out if we can't control an LED */
  143. if (!led_config || !led_config->gpio_leds ||
  144. !led_config->num_led_states || !led_config->led_states)
  145. return 0;
  146. if (led < 0) {
  147. /* if LED is already lit, then leave it as-is */
  148. if (state->led_state)
  149. return 0;
  150. else
  151. led *= -1;
  152. }
  153. /* toggle LED if changing state */
  154. if (state->led_state != led) {
  155. u8 val;
  156. dprintk("%s: %d\n", __func__, led);
  157. au8522_led_gpio_enable(state, 1);
  158. val = au8522_readreg(state, 0x4000 |
  159. (led_config->gpio_leds & ~0xc000));
  160. /* start with all leds off */
  161. for (i = 0; i < led_config->num_led_states; i++)
  162. val &= ~led_config->led_states[i];
  163. /* set selected LED state */
  164. if (led < led_config->num_led_states)
  165. val |= led_config->led_states[led];
  166. else if (led_config->num_led_states)
  167. val |=
  168. led_config->led_states[led_config->num_led_states - 1];
  169. ret = au8522_writereg(state, 0x8000 |
  170. (led_config->gpio_leds & ~0xc000), val);
  171. if (ret < 0)
  172. return ret;
  173. state->led_state = led;
  174. if (led == 0)
  175. au8522_led_gpio_enable(state, 0);
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(au8522_led_ctrl);
  180. int au8522_init(struct dvb_frontend *fe)
  181. {
  182. struct au8522_state *state = fe->demodulator_priv;
  183. dprintk("%s()\n", __func__);
  184. state->operational_mode = AU8522_DIGITAL_MODE;
  185. /* Clear out any state associated with the digital side of the
  186. chip, so that when it gets powered back up it won't think
  187. that it is already tuned */
  188. state->current_frequency = 0;
  189. au8522_writereg(state, 0xa4, 1 << 5);
  190. au8522_i2c_gate_ctrl(fe, 1);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(au8522_init);
  194. int au8522_sleep(struct dvb_frontend *fe)
  195. {
  196. struct au8522_state *state = fe->demodulator_priv;
  197. dprintk("%s()\n", __func__);
  198. /* Only power down if the digital side is currently using the chip */
  199. if (state->operational_mode == AU8522_ANALOG_MODE) {
  200. /* We're not in one of the expected power modes, which means
  201. that the DVB thread is probably telling us to go to sleep
  202. even though the analog frontend has already started using
  203. the chip. So ignore the request */
  204. return 0;
  205. }
  206. /* turn off led */
  207. au8522_led_ctrl(state, 0);
  208. /* Power down the chip */
  209. au8522_writereg(state, 0xa4, 1 << 5);
  210. state->current_frequency = 0;
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(au8522_sleep);
  214. module_param(debug, int, 0644);
  215. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  216. MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
  217. MODULE_AUTHOR("Steven Toth");
  218. MODULE_LICENSE("GPL");