| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2024 Analog Devices Inc. |
| 4 |
* Copyright (C) 2024 BayLibre, SAS |
| 5 |
* |
| 6 |
* Generic PWM trigger for SPI offload. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/device.h> |
| 10 |
#include <linux/err.h> |
| 11 |
#include <linux/math.h> |
| 12 |
#include <linux/mod_devicetable.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/platform_device.h> |
| 15 |
#include <linux/property.h> |
| 16 |
#include <linux/pwm.h> |
| 17 |
#include <linux/spi/offload/provider.h> |
| 18 |
#include <linux/spi/offload/types.h> |
| 19 |
#include <linux/time.h> |
| 20 |
#include <linux/types.h> |
| 21 |
|
| 22 |
struct spi_offload_trigger_pwm_state { |
| 23 |
struct device *dev; |
| 24 |
struct pwm_device *pwm; |
| 25 |
}; |
| 26 |
|
| 27 |
static bool spi_offload_trigger_pwm_match(struct spi_offload_trigger *trigger, |
| 28 |
enum spi_offload_trigger_type type, |
| 29 |
u64 *args, u32 nargs) |
| 30 |
{ |
| 31 |
if (nargs) |
| 32 |
return false; |
| 33 |
|
| 34 |
return type == SPI_OFFLOAD_TRIGGER_PERIODIC; |
| 35 |
} |
| 36 |
|
| 37 |
static int spi_offload_trigger_pwm_validate(struct spi_offload_trigger *trigger, |
| 38 |
struct spi_offload_trigger_config *config) |
| 39 |
{ |
| 40 |
struct spi_offload_trigger_pwm_state *st = spi_offload_trigger_get_priv(trigger); |
| 41 |
struct spi_offload_trigger_periodic *periodic = &config->periodic; |
| 42 |
struct pwm_waveform wf = { }; |
| 43 |
int ret; |
| 44 |
|
| 45 |
if (config->type != SPI_OFFLOAD_TRIGGER_PERIODIC) |
| 46 |
return -EINVAL; |
| 47 |
|
| 48 |
if (!periodic->frequency_hz) |
| 49 |
return -EINVAL; |
| 50 |
|
| 51 |
wf.period_length_ns = DIV_ROUND_UP_ULL(NSEC_PER_SEC, periodic->frequency_hz); |
| 52 |
/* REVISIT: 50% duty-cycle for now - may add config parameter later */ |
| 53 |
wf.duty_length_ns = wf.period_length_ns / 2; |
| 54 |
wf.duty_offset_ns = periodic->offset_ns; |
| 55 |
|
| 56 |
ret = pwm_round_waveform_might_sleep(st->pwm, &wf); |
| 57 |
if (ret < 0) |
| 58 |
return ret; |
| 59 |
|
| 60 |
periodic->frequency_hz = DIV_ROUND_UP_ULL(NSEC_PER_SEC, wf.period_length_ns); |
| 61 |
periodic->offset_ns = wf.duty_offset_ns; |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static int spi_offload_trigger_pwm_enable(struct spi_offload_trigger *trigger, |
| 67 |
struct spi_offload_trigger_config *config) |
| 68 |
{ |
| 69 |
struct spi_offload_trigger_pwm_state *st = spi_offload_trigger_get_priv(trigger); |
| 70 |
struct spi_offload_trigger_periodic *periodic = &config->periodic; |
| 71 |
struct pwm_waveform wf = { }; |
| 72 |
|
| 73 |
if (config->type != SPI_OFFLOAD_TRIGGER_PERIODIC) |
| 74 |
return -EINVAL; |
| 75 |
|
| 76 |
if (!periodic->frequency_hz) |
| 77 |
return -EINVAL; |
| 78 |
|
| 79 |
wf.period_length_ns = DIV_ROUND_UP_ULL(NSEC_PER_SEC, periodic->frequency_hz); |
| 80 |
/* REVISIT: 50% duty-cycle for now - may add config parameter later */ |
| 81 |
wf.duty_length_ns = wf.period_length_ns / 2; |
| 82 |
wf.duty_offset_ns = periodic->offset_ns; |
| 83 |
|
| 84 |
return pwm_set_waveform_might_sleep(st->pwm, &wf, false); |
| 85 |
} |
| 86 |
|
| 87 |
static void spi_offload_trigger_pwm_disable(struct spi_offload_trigger *trigger) |
| 88 |
{ |
| 89 |
struct spi_offload_trigger_pwm_state *st = spi_offload_trigger_get_priv(trigger); |
| 90 |
struct pwm_waveform wf; |
| 91 |
int ret; |
| 92 |
|
| 93 |
ret = pwm_get_waveform_might_sleep(st->pwm, &wf); |
| 94 |
if (ret < 0) { |
| 95 |
dev_err(st->dev, "failed to get waveform: %d\n", ret); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
wf.duty_length_ns = 0; |
| 100 |
|
| 101 |
ret = pwm_set_waveform_might_sleep(st->pwm, &wf, false); |
| 102 |
if (ret < 0) |
| 103 |
dev_err(st->dev, "failed to disable PWM: %d\n", ret); |
| 104 |
} |
| 105 |
|
| 106 |
static const struct spi_offload_trigger_ops spi_offload_trigger_pwm_ops = { |
| 107 |
.match = spi_offload_trigger_pwm_match, |
| 108 |
.validate = spi_offload_trigger_pwm_validate, |
| 109 |
.enable = spi_offload_trigger_pwm_enable, |
| 110 |
.disable = spi_offload_trigger_pwm_disable, |
| 111 |
}; |
| 112 |
|
| 113 |
static void spi_offload_trigger_pwm_release(void *data) |
| 114 |
{ |
| 115 |
pwm_disable(data); |
| 116 |
} |
| 117 |
|
| 118 |
static int spi_offload_trigger_pwm_probe(struct platform_device *pdev) |
| 119 |
{ |
| 120 |
struct device *dev = &pdev->dev; |
| 121 |
struct spi_offload_trigger_info info = { |
| 122 |
.fwnode = dev_fwnode(dev), |
| 123 |
.ops = &spi_offload_trigger_pwm_ops, |
| 124 |
}; |
| 125 |
struct spi_offload_trigger_pwm_state *st; |
| 126 |
struct pwm_state state; |
| 127 |
int ret; |
| 128 |
|
| 129 |
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); |
| 130 |
if (!st) |
| 131 |
return -ENOMEM; |
| 132 |
|
| 133 |
info.priv = st; |
| 134 |
st->dev = dev; |
| 135 |
|
| 136 |
st->pwm = devm_pwm_get(dev, NULL); |
| 137 |
if (IS_ERR(st->pwm)) |
| 138 |
return dev_err_probe(dev, PTR_ERR(st->pwm), "failed to get PWM\n"); |
| 139 |
|
| 140 |
/* init with duty_cycle = 0, output enabled to ensure trigger off */ |
| 141 |
pwm_init_state(st->pwm, &state); |
| 142 |
state.enabled = true; |
| 143 |
|
| 144 |
ret = pwm_apply_might_sleep(st->pwm, &state); |
| 145 |
if (ret < 0) |
| 146 |
return dev_err_probe(dev, ret, "failed to apply PWM state\n"); |
| 147 |
|
| 148 |
ret = devm_add_action_or_reset(dev, spi_offload_trigger_pwm_release, st->pwm); |
| 149 |
if (ret) |
| 150 |
return ret; |
| 151 |
|
| 152 |
return devm_spi_offload_trigger_register(dev, &info); |
| 153 |
} |
| 154 |
|
| 155 |
static const struct of_device_id spi_offload_trigger_pwm_of_match_table[] = { |
| 156 |
{ .compatible = "pwm-trigger" }, |
| 157 |
{ } |
| 158 |
}; |
| 159 |
MODULE_DEVICE_TABLE(of, spi_offload_trigger_pwm_of_match_table); |
| 160 |
|
| 161 |
static struct platform_driver spi_offload_trigger_pwm_driver = { |
| 162 |
.driver = { |
| 163 |
.name = "pwm-trigger", |
| 164 |
.of_match_table = spi_offload_trigger_pwm_of_match_table, |
| 165 |
}, |
| 166 |
.probe = spi_offload_trigger_pwm_probe, |
| 167 |
}; |
| 168 |
module_platform_driver(spi_offload_trigger_pwm_driver); |
| 169 |
|
| 170 |
MODULE_AUTHOR("David Lechner <[email protected]>"); |
| 171 |
MODULE_DESCRIPTION("Generic PWM trigger"); |
| 172 |
MODULE_LICENSE("GPL"); |