isl6421.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * isl6421.h - driver for lnb supply and control ic ISL6421
  4. *
  5. * Copyright (C) 2006 Andrew de Quincey
  6. * Copyright (C) 2006 Oliver Endriss
  7. *
  8. * the project's page is at https://linuxtv.org
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/slab.h>
  17. #include <media/dvb_frontend.h>
  18. #include "isl6421.h"
  19. struct isl6421 {
  20. u8 config;
  21. u8 override_or;
  22. u8 override_and;
  23. struct i2c_adapter *i2c;
  24. u8 i2c_addr;
  25. bool is_off;
  26. };
  27. static int isl6421_set_voltage(struct dvb_frontend *fe,
  28. enum fe_sec_voltage voltage)
  29. {
  30. int ret;
  31. u8 buf;
  32. bool is_off;
  33. struct isl6421 *isl6421 = (struct isl6421 *) fe->sec_priv;
  34. struct i2c_msg msg[2] = {
  35. {
  36. .addr = isl6421->i2c_addr,
  37. .flags = 0,
  38. .buf = &isl6421->config,
  39. .len = 1,
  40. }, {
  41. .addr = isl6421->i2c_addr,
  42. .flags = I2C_M_RD,
  43. .buf = &buf,
  44. .len = 1,
  45. }
  46. };
  47. isl6421->config &= ~(ISL6421_VSEL1 | ISL6421_EN1);
  48. switch(voltage) {
  49. case SEC_VOLTAGE_OFF:
  50. is_off = true;
  51. break;
  52. case SEC_VOLTAGE_13:
  53. is_off = false;
  54. isl6421->config |= ISL6421_EN1;
  55. break;
  56. case SEC_VOLTAGE_18:
  57. is_off = false;
  58. isl6421->config |= (ISL6421_EN1 | ISL6421_VSEL1);
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. /*
  64. * If LNBf were not powered on, disable dynamic current limit, as,
  65. * according with datasheet, highly capacitive load on the output may
  66. * cause a difficult start-up.
  67. */
  68. if (isl6421->is_off && !is_off)
  69. isl6421->config |= ISL6421_DCL;
  70. isl6421->config |= isl6421->override_or;
  71. isl6421->config &= isl6421->override_and;
  72. ret = i2c_transfer(isl6421->i2c, msg, 2);
  73. if (ret < 0)
  74. return ret;
  75. if (ret != 2)
  76. return -EIO;
  77. /* Store off status now in case future commands fail */
  78. isl6421->is_off = is_off;
  79. /* On overflow, the device will try again after 900 ms (typically) */
  80. if (!is_off && (buf & ISL6421_OLF1))
  81. msleep(1000);
  82. /* Re-enable dynamic current limit */
  83. if ((isl6421->config & ISL6421_DCL) &&
  84. !(isl6421->override_or & ISL6421_DCL)) {
  85. isl6421->config &= ~ISL6421_DCL;
  86. ret = i2c_transfer(isl6421->i2c, msg, 2);
  87. if (ret < 0)
  88. return ret;
  89. if (ret != 2)
  90. return -EIO;
  91. }
  92. /* Check if overload flag is active. If so, disable power */
  93. if (!is_off && (buf & ISL6421_OLF1)) {
  94. isl6421->config &= ~(ISL6421_VSEL1 | ISL6421_EN1);
  95. ret = i2c_transfer(isl6421->i2c, msg, 1);
  96. if (ret < 0)
  97. return ret;
  98. if (ret != 1)
  99. return -EIO;
  100. isl6421->is_off = true;
  101. dev_warn(&isl6421->i2c->dev,
  102. "Overload current detected. disabling LNBf power\n");
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. static int isl6421_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
  108. {
  109. struct isl6421 *isl6421 = (struct isl6421 *) fe->sec_priv;
  110. struct i2c_msg msg = { .addr = isl6421->i2c_addr, .flags = 0,
  111. .buf = &isl6421->config,
  112. .len = sizeof(isl6421->config) };
  113. if (arg)
  114. isl6421->config |= ISL6421_LLC1;
  115. else
  116. isl6421->config &= ~ISL6421_LLC1;
  117. isl6421->config |= isl6421->override_or;
  118. isl6421->config &= isl6421->override_and;
  119. return (i2c_transfer(isl6421->i2c, &msg, 1) == 1) ? 0 : -EIO;
  120. }
  121. static int isl6421_set_tone(struct dvb_frontend *fe,
  122. enum fe_sec_tone_mode tone)
  123. {
  124. struct isl6421 *isl6421 = (struct isl6421 *) fe->sec_priv;
  125. struct i2c_msg msg = { .addr = isl6421->i2c_addr, .flags = 0,
  126. .buf = &isl6421->config,
  127. .len = sizeof(isl6421->config) };
  128. switch (tone) {
  129. case SEC_TONE_ON:
  130. isl6421->config |= ISL6421_ENT1;
  131. break;
  132. case SEC_TONE_OFF:
  133. isl6421->config &= ~ISL6421_ENT1;
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. isl6421->config |= isl6421->override_or;
  139. isl6421->config &= isl6421->override_and;
  140. return (i2c_transfer(isl6421->i2c, &msg, 1) == 1) ? 0 : -EIO;
  141. }
  142. static void isl6421_release(struct dvb_frontend *fe)
  143. {
  144. /* power off */
  145. isl6421_set_voltage(fe, SEC_VOLTAGE_OFF);
  146. /* free */
  147. kfree(fe->sec_priv);
  148. fe->sec_priv = NULL;
  149. }
  150. struct dvb_frontend *isl6421_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, u8 i2c_addr,
  151. u8 override_set, u8 override_clear, bool override_tone)
  152. {
  153. struct isl6421 *isl6421 = kmalloc(sizeof(struct isl6421), GFP_KERNEL);
  154. if (!isl6421)
  155. return NULL;
  156. /* default configuration */
  157. isl6421->config = ISL6421_ISEL1;
  158. isl6421->i2c = i2c;
  159. isl6421->i2c_addr = i2c_addr;
  160. fe->sec_priv = isl6421;
  161. /* bits which should be forced to '1' */
  162. isl6421->override_or = override_set;
  163. /* bits which should be forced to '0' */
  164. isl6421->override_and = ~override_clear;
  165. /* detect if it is present or not */
  166. if (isl6421_set_voltage(fe, SEC_VOLTAGE_OFF)) {
  167. kfree(isl6421);
  168. fe->sec_priv = NULL;
  169. return NULL;
  170. }
  171. isl6421->is_off = true;
  172. /* install release callback */
  173. fe->ops.release_sec = isl6421_release;
  174. /* override frontend ops */
  175. fe->ops.set_voltage = isl6421_set_voltage;
  176. fe->ops.enable_high_lnb_voltage = isl6421_enable_high_lnb_voltage;
  177. if (override_tone)
  178. fe->ops.set_tone = isl6421_set_tone;
  179. return fe;
  180. }
  181. EXPORT_SYMBOL(isl6421_attach);
  182. MODULE_DESCRIPTION("Driver for lnb supply and control ic isl6421");
  183. MODULE_AUTHOR("Andrew de Quincey & Oliver Endriss");
  184. MODULE_LICENSE("GPL");