18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (c) 2019, Linaro Limited
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/module.h>
58c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
68c2ecf20Sopenharmony_ci#include <linux/regmap.h>
78c2ecf20Sopenharmony_ci#include <linux/slab.h>
88c2ecf20Sopenharmony_ci#include <linux/of_device.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define WCD_PIN_MASK(p) BIT(p)
118c2ecf20Sopenharmony_ci#define WCD_REG_DIR_CTL_OFFSET 0x42
128c2ecf20Sopenharmony_ci#define WCD_REG_VAL_CTL_OFFSET 0x43
138c2ecf20Sopenharmony_ci#define WCD934X_NPINS		5
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct wcd_gpio_data {
168c2ecf20Sopenharmony_ci	struct regmap *map;
178c2ecf20Sopenharmony_ci	struct gpio_chip chip;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int wcd_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data = gpiochip_get_data(chip);
238c2ecf20Sopenharmony_ci	unsigned int value;
248c2ecf20Sopenharmony_ci	int ret;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	ret = regmap_read(data->map, WCD_REG_DIR_CTL_OFFSET, &value);
278c2ecf20Sopenharmony_ci	if (ret < 0)
288c2ecf20Sopenharmony_ci		return ret;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	if (value & WCD_PIN_MASK(pin))
318c2ecf20Sopenharmony_ci		return GPIO_LINE_DIRECTION_OUT;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	return GPIO_LINE_DIRECTION_IN;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int wcd_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data = gpiochip_get_data(chip);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return regmap_update_bits(data->map, WCD_REG_DIR_CTL_OFFSET,
418c2ecf20Sopenharmony_ci				  WCD_PIN_MASK(pin), 0);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int wcd_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
458c2ecf20Sopenharmony_ci				     int val)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data = gpiochip_get_data(chip);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	regmap_update_bits(data->map, WCD_REG_DIR_CTL_OFFSET,
508c2ecf20Sopenharmony_ci			   WCD_PIN_MASK(pin), WCD_PIN_MASK(pin));
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return regmap_update_bits(data->map, WCD_REG_VAL_CTL_OFFSET,
538c2ecf20Sopenharmony_ci				  WCD_PIN_MASK(pin),
548c2ecf20Sopenharmony_ci				  val ? WCD_PIN_MASK(pin) : 0);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int wcd_gpio_get(struct gpio_chip *chip, unsigned int pin)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data = gpiochip_get_data(chip);
608c2ecf20Sopenharmony_ci	unsigned int value;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	regmap_read(data->map, WCD_REG_VAL_CTL_OFFSET, &value);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return !!(value & WCD_PIN_MASK(pin));
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void wcd_gpio_set(struct gpio_chip *chip, unsigned int pin, int val)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data = gpiochip_get_data(chip);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	regmap_update_bits(data->map, WCD_REG_VAL_CTL_OFFSET,
728c2ecf20Sopenharmony_ci			   WCD_PIN_MASK(pin), val ? WCD_PIN_MASK(pin) : 0);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int wcd_gpio_probe(struct platform_device *pdev)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
788c2ecf20Sopenharmony_ci	struct wcd_gpio_data *data;
798c2ecf20Sopenharmony_ci	struct gpio_chip *chip;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
828c2ecf20Sopenharmony_ci	if (!data)
838c2ecf20Sopenharmony_ci		return -ENOMEM;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	data->map = dev_get_regmap(dev->parent, NULL);
868c2ecf20Sopenharmony_ci	if (!data->map) {
878c2ecf20Sopenharmony_ci		dev_err(dev, "%s: failed to get regmap\n", __func__);
888c2ecf20Sopenharmony_ci		return  -EINVAL;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	chip = &data->chip;
928c2ecf20Sopenharmony_ci	chip->direction_input  = wcd_gpio_direction_input;
938c2ecf20Sopenharmony_ci	chip->direction_output = wcd_gpio_direction_output;
948c2ecf20Sopenharmony_ci	chip->get_direction = wcd_gpio_get_direction;
958c2ecf20Sopenharmony_ci	chip->get = wcd_gpio_get;
968c2ecf20Sopenharmony_ci	chip->set = wcd_gpio_set;
978c2ecf20Sopenharmony_ci	chip->parent = dev;
988c2ecf20Sopenharmony_ci	chip->base = -1;
998c2ecf20Sopenharmony_ci	chip->ngpio = WCD934X_NPINS;
1008c2ecf20Sopenharmony_ci	chip->label = dev_name(dev);
1018c2ecf20Sopenharmony_ci	chip->of_gpio_n_cells = 2;
1028c2ecf20Sopenharmony_ci	chip->can_sleep = false;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return devm_gpiochip_add_data(dev, chip, data);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic const struct of_device_id wcd_gpio_of_match[] = {
1088c2ecf20Sopenharmony_ci	{ .compatible = "qcom,wcd9340-gpio" },
1098c2ecf20Sopenharmony_ci	{ .compatible = "qcom,wcd9341-gpio" },
1108c2ecf20Sopenharmony_ci	{ }
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, wcd_gpio_of_match);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct platform_driver wcd_gpio_driver = {
1158c2ecf20Sopenharmony_ci	.driver = {
1168c2ecf20Sopenharmony_ci		   .name = "wcd934x-gpio",
1178c2ecf20Sopenharmony_ci		   .of_match_table = wcd_gpio_of_match,
1188c2ecf20Sopenharmony_ci	},
1198c2ecf20Sopenharmony_ci	.probe = wcd_gpio_probe,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cimodule_platform_driver(wcd_gpio_driver);
1238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Qualcomm Technologies, Inc WCD GPIO control driver");
1248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
125