0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/pwm.h>
0013 #include <linux/greybus.h>
0014
0015 #include "gbphy.h"
0016
0017 struct gb_pwm_chip {
0018 struct gb_connection *connection;
0019 u8 pwm_max;
0020
0021 struct pwm_chip chip;
0022 struct pwm_chip *pwm;
0023 };
0024 #define pwm_chip_to_gb_pwm_chip(chip) \
0025 container_of(chip, struct gb_pwm_chip, chip)
0026
0027
0028 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
0029 {
0030 struct gb_pwm_count_response response;
0031 int ret;
0032
0033 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
0034 NULL, 0, &response, sizeof(response));
0035 if (ret)
0036 return ret;
0037 pwmc->pwm_max = response.count;
0038 return 0;
0039 }
0040
0041 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
0042 u8 which)
0043 {
0044 struct gb_pwm_activate_request request;
0045 struct gbphy_device *gbphy_dev;
0046 int ret;
0047
0048 if (which > pwmc->pwm_max)
0049 return -EINVAL;
0050
0051 request.which = which;
0052
0053 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0054 ret = gbphy_runtime_get_sync(gbphy_dev);
0055 if (ret)
0056 return ret;
0057
0058 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
0059 &request, sizeof(request), NULL, 0);
0060
0061 gbphy_runtime_put_autosuspend(gbphy_dev);
0062
0063 return ret;
0064 }
0065
0066 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
0067 u8 which)
0068 {
0069 struct gb_pwm_deactivate_request request;
0070 struct gbphy_device *gbphy_dev;
0071 int ret;
0072
0073 if (which > pwmc->pwm_max)
0074 return -EINVAL;
0075
0076 request.which = which;
0077
0078 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0079 ret = gbphy_runtime_get_sync(gbphy_dev);
0080 if (ret)
0081 return ret;
0082
0083 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
0084 &request, sizeof(request), NULL, 0);
0085
0086 gbphy_runtime_put_autosuspend(gbphy_dev);
0087
0088 return ret;
0089 }
0090
0091 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
0092 u8 which, u32 duty, u32 period)
0093 {
0094 struct gb_pwm_config_request request;
0095 struct gbphy_device *gbphy_dev;
0096 int ret;
0097
0098 if (which > pwmc->pwm_max)
0099 return -EINVAL;
0100
0101 request.which = which;
0102 request.duty = cpu_to_le32(duty);
0103 request.period = cpu_to_le32(period);
0104
0105 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0106 ret = gbphy_runtime_get_sync(gbphy_dev);
0107 if (ret)
0108 return ret;
0109
0110 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
0111 &request, sizeof(request), NULL, 0);
0112
0113 gbphy_runtime_put_autosuspend(gbphy_dev);
0114
0115 return ret;
0116 }
0117
0118 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
0119 u8 which, u8 polarity)
0120 {
0121 struct gb_pwm_polarity_request request;
0122 struct gbphy_device *gbphy_dev;
0123 int ret;
0124
0125 if (which > pwmc->pwm_max)
0126 return -EINVAL;
0127
0128 request.which = which;
0129 request.polarity = polarity;
0130
0131 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0132 ret = gbphy_runtime_get_sync(gbphy_dev);
0133 if (ret)
0134 return ret;
0135
0136 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
0137 &request, sizeof(request), NULL, 0);
0138
0139 gbphy_runtime_put_autosuspend(gbphy_dev);
0140
0141 return ret;
0142 }
0143
0144 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
0145 u8 which)
0146 {
0147 struct gb_pwm_enable_request request;
0148 struct gbphy_device *gbphy_dev;
0149 int ret;
0150
0151 if (which > pwmc->pwm_max)
0152 return -EINVAL;
0153
0154 request.which = which;
0155
0156 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0157 ret = gbphy_runtime_get_sync(gbphy_dev);
0158 if (ret)
0159 return ret;
0160
0161 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
0162 &request, sizeof(request), NULL, 0);
0163 if (ret)
0164 gbphy_runtime_put_autosuspend(gbphy_dev);
0165
0166 return ret;
0167 }
0168
0169 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
0170 u8 which)
0171 {
0172 struct gb_pwm_disable_request request;
0173 struct gbphy_device *gbphy_dev;
0174 int ret;
0175
0176 if (which > pwmc->pwm_max)
0177 return -EINVAL;
0178
0179 request.which = which;
0180
0181 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
0182 &request, sizeof(request), NULL, 0);
0183
0184 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
0185 gbphy_runtime_put_autosuspend(gbphy_dev);
0186
0187 return ret;
0188 }
0189
0190 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
0191 {
0192 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
0193
0194 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
0195 };
0196
0197 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
0198 {
0199 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
0200
0201 if (pwm_is_enabled(pwm))
0202 dev_warn(chip->dev, "freeing PWM device without disabling\n");
0203
0204 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
0205 }
0206
0207 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0208 const struct pwm_state *state)
0209 {
0210 int err;
0211 bool enabled = pwm->state.enabled;
0212 u64 period = state->period;
0213 u64 duty_cycle = state->duty_cycle;
0214 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
0215
0216
0217 if (state->polarity != pwm->state.polarity) {
0218 if (enabled) {
0219 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
0220 enabled = false;
0221 }
0222 err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity);
0223 if (err)
0224 return err;
0225 }
0226
0227 if (!state->enabled) {
0228 if (enabled)
0229 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
0230 return 0;
0231 }
0232
0233
0234
0235
0236
0237
0238
0239 if (period > U32_MAX)
0240 period = U32_MAX;
0241
0242 if (duty_cycle > period)
0243 duty_cycle = period;
0244
0245 err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period);
0246 if (err)
0247 return err;
0248
0249
0250 if (!enabled)
0251 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
0252
0253 return 0;
0254 }
0255
0256 static const struct pwm_ops gb_pwm_ops = {
0257 .request = gb_pwm_request,
0258 .free = gb_pwm_free,
0259 .apply = gb_pwm_apply,
0260 .owner = THIS_MODULE,
0261 };
0262
0263 static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
0264 const struct gbphy_device_id *id)
0265 {
0266 struct gb_connection *connection;
0267 struct gb_pwm_chip *pwmc;
0268 struct pwm_chip *pwm;
0269 int ret;
0270
0271 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
0272 if (!pwmc)
0273 return -ENOMEM;
0274
0275 connection = gb_connection_create(gbphy_dev->bundle,
0276 le16_to_cpu(gbphy_dev->cport_desc->id),
0277 NULL);
0278 if (IS_ERR(connection)) {
0279 ret = PTR_ERR(connection);
0280 goto exit_pwmc_free;
0281 }
0282
0283 pwmc->connection = connection;
0284 gb_connection_set_data(connection, pwmc);
0285 gb_gbphy_set_data(gbphy_dev, pwmc);
0286
0287 ret = gb_connection_enable(connection);
0288 if (ret)
0289 goto exit_connection_destroy;
0290
0291
0292 ret = gb_pwm_count_operation(pwmc);
0293 if (ret)
0294 goto exit_connection_disable;
0295
0296 pwm = &pwmc->chip;
0297
0298 pwm->dev = &gbphy_dev->dev;
0299 pwm->ops = &gb_pwm_ops;
0300 pwm->npwm = pwmc->pwm_max + 1;
0301
0302 ret = pwmchip_add(pwm);
0303 if (ret) {
0304 dev_err(&gbphy_dev->dev,
0305 "failed to register PWM: %d\n", ret);
0306 goto exit_connection_disable;
0307 }
0308
0309 gbphy_runtime_put_autosuspend(gbphy_dev);
0310 return 0;
0311
0312 exit_connection_disable:
0313 gb_connection_disable(connection);
0314 exit_connection_destroy:
0315 gb_connection_destroy(connection);
0316 exit_pwmc_free:
0317 kfree(pwmc);
0318 return ret;
0319 }
0320
0321 static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
0322 {
0323 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
0324 struct gb_connection *connection = pwmc->connection;
0325 int ret;
0326
0327 ret = gbphy_runtime_get_sync(gbphy_dev);
0328 if (ret)
0329 gbphy_runtime_get_noresume(gbphy_dev);
0330
0331 pwmchip_remove(&pwmc->chip);
0332 gb_connection_disable(connection);
0333 gb_connection_destroy(connection);
0334 kfree(pwmc);
0335 }
0336
0337 static const struct gbphy_device_id gb_pwm_id_table[] = {
0338 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
0339 { },
0340 };
0341 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
0342
0343 static struct gbphy_driver pwm_driver = {
0344 .name = "pwm",
0345 .probe = gb_pwm_probe,
0346 .remove = gb_pwm_remove,
0347 .id_table = gb_pwm_id_table,
0348 };
0349
0350 module_gbphy_driver(pwm_driver);
0351 MODULE_LICENSE("GPL v2");