2017-11-07 13:58:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-11-13 14:14:13 +00:00
|
|
|
/*
|
|
|
|
* PWM Greybus driver.
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
* Copyright 2014 Linaro Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/pwm.h>
|
2019-08-25 05:54:27 +00:00
|
|
|
#include <linux/greybus.h>
|
2015-05-22 17:35:31 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
#include "gbphy.h"
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
struct gb_pwm_chip {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
u8 pwm_max; /* max pwm number */
|
|
|
|
|
|
|
|
struct pwm_chip chip;
|
|
|
|
struct pwm_chip *pwm;
|
|
|
|
};
|
2023-03-26 06:45:19 +00:00
|
|
|
|
|
|
|
static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip)
|
|
|
|
{
|
|
|
|
return container_of(chip, struct gb_pwm_chip, chip);
|
|
|
|
}
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_count_response response;
|
2014-11-13 14:14:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
|
|
|
|
NULL, 0, &response, sizeof(response));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
pwmc->pwm_max = response.count;
|
|
|
|
return 0;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_activate_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_deactivate_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which, u32 duty, u32 period)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_config_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2014-12-02 21:48:09 +00:00
|
|
|
request.duty = cpu_to_le32(duty);
|
|
|
|
request.period = cpu_to_le32(period);
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which, u8 polarity)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_polarity_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
|
|
|
request.polarity = polarity;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_enable_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (ret)
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_disable_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
2016-06-08 04:33:54 +00:00
|
|
|
if (pwm_is_enabled(pwm))
|
2014-11-13 14:14:13 +00:00
|
|
|
dev_warn(chip->dev, "freeing PWM device without disabling\n");
|
|
|
|
|
|
|
|
gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
|
|
|
|
}
|
|
|
|
|
2022-03-18 09:57:12 +00:00
|
|
|
static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
const struct pwm_state *state)
|
2014-11-13 14:14:13 +00:00
|
|
|
{
|
2022-03-18 09:57:12 +00:00
|
|
|
int err;
|
|
|
|
bool enabled = pwm->state.enabled;
|
|
|
|
u64 period = state->period;
|
|
|
|
u64 duty_cycle = state->duty_cycle;
|
2014-11-13 14:14:13 +00:00
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
2022-03-18 09:57:12 +00:00
|
|
|
/* Set polarity */
|
|
|
|
if (state->polarity != pwm->state.polarity) {
|
|
|
|
if (enabled) {
|
|
|
|
gb_pwm_disable_operation(pwmc, pwm->hwpwm);
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state->enabled) {
|
|
|
|
if (enabled)
|
|
|
|
gb_pwm_disable_operation(pwmc, pwm->hwpwm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set period and duty cycle
|
|
|
|
*
|
|
|
|
* PWM privodes 64-bit period and duty_cycle, but greybus only accepts
|
|
|
|
* 32-bit, so their values have to be limited to U32_MAX.
|
|
|
|
*/
|
|
|
|
if (period > U32_MAX)
|
|
|
|
period = U32_MAX;
|
|
|
|
|
|
|
|
if (duty_cycle > period)
|
|
|
|
duty_cycle = period;
|
|
|
|
|
|
|
|
err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* enable/disable */
|
|
|
|
if (!enabled)
|
|
|
|
return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
|
2014-11-13 14:14:13 +00:00
|
|
|
|
2022-03-18 09:57:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
static const struct pwm_ops gb_pwm_ops = {
|
|
|
|
.request = gb_pwm_request,
|
|
|
|
.free = gb_pwm_free,
|
2022-03-18 09:57:12 +00:00
|
|
|
.apply = gb_pwm_apply,
|
2014-11-13 14:14:13 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
|
|
|
|
const struct gbphy_device_id *id)
|
2014-11-13 14:14:13 +00:00
|
|
|
{
|
2016-05-05 09:02:31 +00:00
|
|
|
struct gb_connection *connection;
|
2014-11-13 14:14:13 +00:00
|
|
|
struct gb_pwm_chip *pwmc;
|
|
|
|
struct pwm_chip *pwm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
|
|
|
|
if (!pwmc)
|
|
|
|
return -ENOMEM;
|
2016-05-05 09:02:31 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
connection = gb_connection_create(gbphy_dev->bundle,
|
|
|
|
le16_to_cpu(gbphy_dev->cport_desc->id),
|
2016-05-05 09:02:31 +00:00
|
|
|
NULL);
|
|
|
|
if (IS_ERR(connection)) {
|
|
|
|
ret = PTR_ERR(connection);
|
|
|
|
goto exit_pwmc_free;
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:14:13 +00:00
|
|
|
pwmc->connection = connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
gb_connection_set_data(connection, pwmc);
|
2016-05-19 15:52:39 +00:00
|
|
|
gb_gbphy_set_data(gbphy_dev, pwmc);
|
2016-05-05 09:02:31 +00:00
|
|
|
|
|
|
|
ret = gb_connection_enable(connection);
|
|
|
|
if (ret)
|
|
|
|
goto exit_connection_destroy;
|
|
|
|
|
2014-11-13 14:14:13 +00:00
|
|
|
/* Query number of pwms present */
|
|
|
|
ret = gb_pwm_count_operation(pwmc);
|
|
|
|
if (ret)
|
2016-05-05 09:02:31 +00:00
|
|
|
goto exit_connection_disable;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
pwm = &pwmc->chip;
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
pwm->dev = &gbphy_dev->dev;
|
2014-11-13 14:14:13 +00:00
|
|
|
pwm->ops = &gb_pwm_ops;
|
|
|
|
pwm->npwm = pwmc->pwm_max + 1;
|
|
|
|
|
|
|
|
ret = pwmchip_add(pwm);
|
|
|
|
if (ret) {
|
2016-05-19 15:52:39 +00:00
|
|
|
dev_err(&gbphy_dev->dev,
|
2015-10-14 18:18:50 +00:00
|
|
|
"failed to register PWM: %d\n", ret);
|
2016-05-05 09:02:31 +00:00
|
|
|
goto exit_connection_disable;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
2014-11-13 14:14:13 +00:00
|
|
|
return 0;
|
2016-05-05 09:02:31 +00:00
|
|
|
|
|
|
|
exit_connection_disable:
|
|
|
|
gb_connection_disable(connection);
|
|
|
|
exit_connection_destroy:
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
exit_pwmc_free:
|
2014-11-13 14:14:13 +00:00
|
|
|
kfree(pwmc);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
|
2014-11-13 14:14:13 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
|
2016-05-05 09:02:31 +00:00
|
|
|
struct gb_connection *connection = pwmc->connection;
|
2016-07-14 20:13:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
gbphy_runtime_get_noresume(gbphy_dev);
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
pwmchip_remove(&pwmc->chip);
|
2016-05-05 09:02:31 +00:00
|
|
|
gb_connection_disable(connection);
|
|
|
|
gb_connection_destroy(connection);
|
2014-11-13 14:14:13 +00:00
|
|
|
kfree(pwmc);
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static const struct gbphy_device_id gb_pwm_id_table[] = {
|
|
|
|
{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
|
2016-05-05 09:02:31 +00:00
|
|
|
{ },
|
2014-11-13 14:14:13 +00:00
|
|
|
};
|
2016-05-19 15:52:39 +00:00
|
|
|
MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
|
2014-11-13 14:14:13 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static struct gbphy_driver pwm_driver = {
|
2016-05-05 09:02:31 +00:00
|
|
|
.name = "pwm",
|
|
|
|
.probe = gb_pwm_probe,
|
|
|
|
.remove = gb_pwm_remove,
|
|
|
|
.id_table = gb_pwm_id_table,
|
|
|
|
};
|
2016-05-09 12:45:08 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
module_gbphy_driver(pwm_driver);
|
2016-05-09 12:45:08 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|