pwm-mc33xs2410.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
  4. *
  5. * Reference Manual : https://www.nxp.com/docs/en/data-sheet/MC33XS2410.pdf
  6. *
  7. * Limitations:
  8. * - Supports frequencies between 0.5Hz and 2048Hz with following steps:
  9. * - 0.5 Hz steps from 0.5 Hz to 32 Hz
  10. * - 2 Hz steps from 2 Hz to 128 Hz
  11. * - 8 Hz steps from 8 Hz to 512 Hz
  12. * - 32 Hz steps from 32 Hz to 2048 Hz
  13. * - Cannot generate a 0 % duty cycle.
  14. * - Always produces low output if disabled.
  15. * - Configuration isn't atomic. When changing polarity, duty cycle or period
  16. * the data is taken immediately, counters not being affected, resulting in a
  17. * behavior of the output pin that is neither the old nor the new state,
  18. * rather something in between.
  19. */
  20. #define DEFAULT_SYMBOL_NAMESPACE "PWM_MC33XS2410"
  21. #include <linux/auxiliary_bus.h>
  22. #include <linux/bitfield.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/math64.h>
  26. #include <linux/mc33xs2410.h>
  27. #include <linux/minmax.h>
  28. #include <linux/module.h>
  29. #include <linux/of.h>
  30. #include <linux/pwm.h>
  31. #include <linux/spi/spi.h>
  32. #define MC33XS2410_GLB_CTRL 0x00
  33. #define MC33XS2410_GLB_CTRL_MODE GENMASK(7, 6)
  34. #define MC33XS2410_GLB_CTRL_MODE_NORMAL FIELD_PREP(MC33XS2410_GLB_CTRL_MODE, 1)
  35. #define MC33XS2410_PWM_CTRL1 0x05
  36. /* chan in { 1 ... 4 } */
  37. #define MC33XS2410_PWM_CTRL1_POL_INV(chan) BIT((chan) + 1)
  38. #define MC33XS2410_PWM_CTRL3 0x07
  39. /* chan in { 1 ... 4 } */
  40. #define MC33XS2410_PWM_CTRL3_EN(chan) BIT(4 + (chan) - 1)
  41. /* chan in { 1 ... 4 } */
  42. #define MC33XS2410_PWM_FREQ(chan) (0x08 + (chan) - 1)
  43. #define MC33XS2410_PWM_FREQ_STEP GENMASK(7, 6)
  44. #define MC33XS2410_PWM_FREQ_COUNT GENMASK(5, 0)
  45. /* chan in { 1 ... 4 } */
  46. #define MC33XS2410_PWM_DC(chan) (0x0c + (chan) - 1)
  47. #define MC33XS2410_WDT 0x14
  48. #define MC33XS2410_PWM_MIN_PERIOD 488282
  49. /* step in { 0 ... 3 } */
  50. #define MC33XS2410_PWM_MAX_PERIOD(step) (2000000000 >> (2 * (step)))
  51. #define MC33XS2410_FRAME_IN_ADDR GENMASK(15, 8)
  52. #define MC33XS2410_FRAME_IN_DATA GENMASK(7, 0)
  53. #define MC33XS2410_FRAME_IN_ADDR_WR BIT(7)
  54. #define MC33XS2410_FRAME_IN_DATA_RD BIT(7)
  55. #define MC33XS2410_FRAME_OUT_DATA GENMASK(13, 0)
  56. #define MC33XS2410_MAX_TRANSFERS 5
  57. static int mc33xs2410_write_regs(struct spi_device *spi, u8 *reg, u8 *val,
  58. unsigned int len)
  59. {
  60. u16 tx[MC33XS2410_MAX_TRANSFERS];
  61. int i;
  62. if (len > MC33XS2410_MAX_TRANSFERS)
  63. return -EINVAL;
  64. for (i = 0; i < len; i++)
  65. tx[i] = FIELD_PREP(MC33XS2410_FRAME_IN_DATA, val[i]) |
  66. FIELD_PREP(MC33XS2410_FRAME_IN_ADDR,
  67. MC33XS2410_FRAME_IN_ADDR_WR | reg[i]);
  68. return spi_write(spi, tx, len * 2);
  69. }
  70. static int mc33xs2410_read_regs(struct spi_device *spi, u8 *reg, u8 flag,
  71. u16 *val, unsigned int len)
  72. {
  73. u16 tx[MC33XS2410_MAX_TRANSFERS];
  74. u16 rx[MC33XS2410_MAX_TRANSFERS];
  75. struct spi_transfer t = {
  76. .tx_buf = tx,
  77. .rx_buf = rx,
  78. };
  79. int i, ret;
  80. len++;
  81. if (len > MC33XS2410_MAX_TRANSFERS)
  82. return -EINVAL;
  83. t.len = len * 2;
  84. for (i = 0; i < len - 1; i++)
  85. tx[i] = FIELD_PREP(MC33XS2410_FRAME_IN_DATA, flag) |
  86. FIELD_PREP(MC33XS2410_FRAME_IN_ADDR, reg[i]);
  87. ret = spi_sync_transfer(spi, &t, 1);
  88. if (ret < 0)
  89. return ret;
  90. for (i = 1; i < len; i++)
  91. val[i - 1] = FIELD_GET(MC33XS2410_FRAME_OUT_DATA, rx[i]);
  92. return 0;
  93. }
  94. static int mc33xs2410_write_reg(struct spi_device *spi, u8 reg, u8 val)
  95. {
  96. return mc33xs2410_write_regs(spi, &reg, &val, 1);
  97. }
  98. static int mc33xs2410_read_reg(struct spi_device *spi, u8 reg, u16 *val, u8 flag)
  99. {
  100. return mc33xs2410_read_regs(spi, &reg, flag, val, 1);
  101. }
  102. int mc33xs2410_read_reg_ctrl(struct spi_device *spi, u8 reg, u16 *val)
  103. {
  104. return mc33xs2410_read_reg(spi, reg, val, MC33XS2410_FRAME_IN_DATA_RD);
  105. }
  106. EXPORT_SYMBOL_GPL(mc33xs2410_read_reg_ctrl);
  107. int mc33xs2410_read_reg_diag(struct spi_device *spi, u8 reg, u16 *val)
  108. {
  109. return mc33xs2410_read_reg(spi, reg, val, 0);
  110. }
  111. EXPORT_SYMBOL_GPL(mc33xs2410_read_reg_diag);
  112. int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, u8 val)
  113. {
  114. u16 tmp;
  115. int ret;
  116. ret = mc33xs2410_read_reg_ctrl(spi, reg, &tmp);
  117. if (ret < 0)
  118. return ret;
  119. tmp &= ~mask;
  120. tmp |= val & mask;
  121. return mc33xs2410_write_reg(spi, reg, tmp);
  122. }
  123. EXPORT_SYMBOL_GPL(mc33xs2410_modify_reg);
  124. static u8 mc33xs2410_pwm_get_freq(u64 period)
  125. {
  126. u8 step, count;
  127. /*
  128. * Check which step [0 .. 3] is appropriate for the given period. The
  129. * period ranges for the different step values overlap. Prefer big step
  130. * values as these allow more finegrained period and duty cycle
  131. * selection.
  132. */
  133. switch (period) {
  134. case MC33XS2410_PWM_MIN_PERIOD ... MC33XS2410_PWM_MAX_PERIOD(3):
  135. step = 3;
  136. break;
  137. case MC33XS2410_PWM_MAX_PERIOD(3) + 1 ... MC33XS2410_PWM_MAX_PERIOD(2):
  138. step = 2;
  139. break;
  140. case MC33XS2410_PWM_MAX_PERIOD(2) + 1 ... MC33XS2410_PWM_MAX_PERIOD(1):
  141. step = 1;
  142. break;
  143. case MC33XS2410_PWM_MAX_PERIOD(1) + 1 ... MC33XS2410_PWM_MAX_PERIOD(0):
  144. step = 0;
  145. break;
  146. }
  147. /*
  148. * Round up here because a higher count results in a higher frequency
  149. * and so a smaller period.
  150. */
  151. count = DIV_ROUND_UP((u32)MC33XS2410_PWM_MAX_PERIOD(step), (u32)period);
  152. return FIELD_PREP(MC33XS2410_PWM_FREQ_STEP, step) |
  153. FIELD_PREP(MC33XS2410_PWM_FREQ_COUNT, count - 1);
  154. }
  155. static u64 mc33xs2410_pwm_get_period(u8 reg)
  156. {
  157. u32 doubled_freq, code, doubled_steps;
  158. /*
  159. * steps:
  160. * - 0 = 0.5Hz
  161. * - 1 = 2Hz
  162. * - 2 = 8Hz
  163. * - 3 = 32Hz
  164. * frequency = (code + 1) x steps.
  165. *
  166. * To avoid losing precision in case steps value is zero, scale the
  167. * steps value for now by two and keep it in mind when calculating the
  168. * period that the frequency had been doubled.
  169. */
  170. doubled_steps = 1 << (FIELD_GET(MC33XS2410_PWM_FREQ_STEP, reg) * 2);
  171. code = FIELD_GET(MC33XS2410_PWM_FREQ_COUNT, reg);
  172. doubled_freq = (code + 1) * doubled_steps;
  173. /* Convert frequency to period, considering the doubled frequency. */
  174. return DIV_ROUND_UP(2 * NSEC_PER_SEC, doubled_freq);
  175. }
  176. /*
  177. * The hardware cannot generate a 0% relative duty cycle for normal and inversed
  178. * polarity. For normal polarity, the channel must be disabled, the device then
  179. * emits a constant low signal.
  180. * For inverted polarity, the channel must be enabled, the polarity must be set
  181. * to normal and the relative duty cylce must be set to 100%. The device then
  182. * emits a constant high signal.
  183. */
  184. static int mc33xs2410_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  185. const struct pwm_state *state)
  186. {
  187. struct spi_device *spi = pwmchip_get_drvdata(chip);
  188. u8 reg[4] = {
  189. MC33XS2410_PWM_FREQ(pwm->hwpwm + 1),
  190. MC33XS2410_PWM_DC(pwm->hwpwm + 1),
  191. MC33XS2410_PWM_CTRL1,
  192. MC33XS2410_PWM_CTRL3
  193. };
  194. u64 period, duty_cycle;
  195. int ret, rel_dc;
  196. u16 rd_val[2];
  197. u8 wr_val[4];
  198. u8 mask;
  199. period = min(state->period, MC33XS2410_PWM_MAX_PERIOD(0));
  200. if (period < MC33XS2410_PWM_MIN_PERIOD)
  201. return -EINVAL;
  202. ret = mc33xs2410_read_regs(spi, &reg[2], MC33XS2410_FRAME_IN_DATA_RD, rd_val, 2);
  203. if (ret < 0)
  204. return ret;
  205. /* Frequency */
  206. wr_val[0] = mc33xs2410_pwm_get_freq(period);
  207. /* Continue calculations with the possibly truncated period */
  208. period = mc33xs2410_pwm_get_period(wr_val[0]);
  209. /* Duty cycle */
  210. duty_cycle = min(period, state->duty_cycle);
  211. rel_dc = div64_u64(duty_cycle * 256, period) - 1;
  212. if (rel_dc >= 0)
  213. wr_val[1] = rel_dc;
  214. else if (state->polarity == PWM_POLARITY_NORMAL)
  215. wr_val[1] = 0;
  216. else
  217. wr_val[1] = 255;
  218. /* Polarity */
  219. mask = MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1);
  220. if (state->polarity == PWM_POLARITY_INVERSED && rel_dc >= 0)
  221. wr_val[2] = rd_val[0] | mask;
  222. else
  223. wr_val[2] = rd_val[0] & ~mask;
  224. /* Enable */
  225. mask = MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1);
  226. if (state->enabled &&
  227. !(state->polarity == PWM_POLARITY_NORMAL && rel_dc < 0))
  228. wr_val[3] = rd_val[1] | mask;
  229. else
  230. wr_val[3] = rd_val[1] & ~mask;
  231. return mc33xs2410_write_regs(spi, reg, wr_val, 4);
  232. }
  233. static int mc33xs2410_pwm_get_state(struct pwm_chip *chip,
  234. struct pwm_device *pwm,
  235. struct pwm_state *state)
  236. {
  237. struct spi_device *spi = pwmchip_get_drvdata(chip);
  238. u8 reg[4] = {
  239. MC33XS2410_PWM_FREQ(pwm->hwpwm + 1),
  240. MC33XS2410_PWM_DC(pwm->hwpwm + 1),
  241. MC33XS2410_PWM_CTRL1,
  242. MC33XS2410_PWM_CTRL3,
  243. };
  244. u16 val[4];
  245. int ret;
  246. ret = mc33xs2410_read_regs(spi, reg, MC33XS2410_FRAME_IN_DATA_RD, val,
  247. ARRAY_SIZE(reg));
  248. if (ret < 0)
  249. return ret;
  250. state->period = mc33xs2410_pwm_get_period(val[0]);
  251. state->polarity = (val[2] & MC33XS2410_PWM_CTRL1_POL_INV(pwm->hwpwm + 1)) ?
  252. PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
  253. state->enabled = !!(val[3] & MC33XS2410_PWM_CTRL3_EN(pwm->hwpwm + 1));
  254. state->duty_cycle = DIV_ROUND_UP_ULL((val[1] + 1) * state->period, 256);
  255. return 0;
  256. }
  257. static const struct pwm_ops mc33xs2410_pwm_ops = {
  258. .apply = mc33xs2410_pwm_apply,
  259. .get_state = mc33xs2410_pwm_get_state,
  260. };
  261. static int mc33xs2410_reset(struct device *dev)
  262. {
  263. struct gpio_desc *reset_gpio;
  264. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  265. if (IS_ERR_OR_NULL(reset_gpio))
  266. return PTR_ERR_OR_ZERO(reset_gpio);
  267. /* Wake-up time */
  268. fsleep(10000);
  269. return 0;
  270. }
  271. static int mc33xs2410_probe(struct spi_device *spi)
  272. {
  273. struct device *dev = &spi->dev;
  274. struct auxiliary_device *adev;
  275. struct pwm_chip *chip;
  276. int ret;
  277. chip = devm_pwmchip_alloc(dev, 4, 0);
  278. if (IS_ERR(chip))
  279. return PTR_ERR(chip);
  280. spi->bits_per_word = 16;
  281. spi->mode |= SPI_CS_WORD;
  282. ret = spi_setup(spi);
  283. if (ret < 0)
  284. return ret;
  285. pwmchip_set_drvdata(chip, spi);
  286. chip->ops = &mc33xs2410_pwm_ops;
  287. /*
  288. * Deasserts the reset of the device. Shouldn't change the output signal
  289. * if the device was setup prior to probing.
  290. */
  291. ret = mc33xs2410_reset(dev);
  292. if (ret)
  293. return ret;
  294. /*
  295. * Disable watchdog and keep in mind that the watchdog won't trigger a
  296. * reset of the machine when running into an timeout, instead the
  297. * control over the outputs is handed over to the INx input logic
  298. * signals of the device. Disabling it here just deactivates this
  299. * feature until a proper solution is found.
  300. */
  301. ret = mc33xs2410_write_reg(spi, MC33XS2410_WDT, 0x0);
  302. if (ret < 0)
  303. return dev_err_probe(dev, ret, "Failed to disable watchdog\n");
  304. /* Transition to normal mode */
  305. ret = mc33xs2410_modify_reg(spi, MC33XS2410_GLB_CTRL,
  306. MC33XS2410_GLB_CTRL_MODE,
  307. MC33XS2410_GLB_CTRL_MODE_NORMAL);
  308. if (ret < 0)
  309. return dev_err_probe(dev, ret,
  310. "Failed to transition to normal mode\n");
  311. ret = devm_pwmchip_add(dev, chip);
  312. if (ret < 0)
  313. return dev_err_probe(dev, ret, "Failed to add pwm chip\n");
  314. adev = devm_auxiliary_device_create(dev, "hwmon", NULL);
  315. if (!adev)
  316. return dev_err_probe(dev, -ENODEV, "Failed to register hwmon device\n");
  317. return 0;
  318. }
  319. static const struct spi_device_id mc33xs2410_spi_id[] = {
  320. { "mc33xs2410" },
  321. { }
  322. };
  323. MODULE_DEVICE_TABLE(spi, mc33xs2410_spi_id);
  324. static const struct of_device_id mc33xs2410_of_match[] = {
  325. { .compatible = "nxp,mc33xs2410" },
  326. { }
  327. };
  328. MODULE_DEVICE_TABLE(of, mc33xs2410_of_match);
  329. static struct spi_driver mc33xs2410_driver = {
  330. .driver = {
  331. .name = "mc33xs2410-pwm",
  332. .of_match_table = mc33xs2410_of_match,
  333. },
  334. .probe = mc33xs2410_probe,
  335. .id_table = mc33xs2410_spi_id,
  336. };
  337. module_spi_driver(mc33xs2410_driver);
  338. MODULE_DESCRIPTION("NXP MC33XS2410 high-side switch driver");
  339. MODULE_AUTHOR("Dimitri Fedrau <dimitri.fedrau@liebherr.com>");
  340. MODULE_LICENSE("GPL");