^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015-2017 Pengutronix, Uwe Kleine-König <kernel@pengutronix.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "siox.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DRIVER_NAME "siox-gpio"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct siox_gpio_ddata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct gpio_desc *din;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct gpio_desc *dout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct gpio_desc *dclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct gpio_desc *dld;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static unsigned int siox_clkhigh_ns = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static unsigned int siox_loadhigh_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static unsigned int siox_bytegap_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int siox_gpio_pushpull(struct siox_master *smaster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) size_t setbuf_len, const u8 setbuf[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) size_t getbuf_len, u8 getbuf[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct siox_gpio_ddata *ddata = siox_master_get_devdata(smaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) size_t cycles = max(setbuf_len, getbuf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* reset data and clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) gpiod_set_value_cansleep(ddata->dout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) gpiod_set_value_cansleep(ddata->dclk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) gpiod_set_value_cansleep(ddata->dld, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ndelay(siox_loadhigh_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) gpiod_set_value_cansleep(ddata->dld, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) for (i = 0; i < cycles; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u8 set = 0, get = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) size_t j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (i >= cycles - setbuf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) set = setbuf[i - (cycles - setbuf_len)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (j = 0; j < 8; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) get <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (gpiod_get_value_cansleep(ddata->din))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) get |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* DOUT is logically inverted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) gpiod_set_value_cansleep(ddata->dout, !(set & 0x80));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) set <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) gpiod_set_value_cansleep(ddata->dclk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ndelay(siox_clkhigh_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) gpiod_set_value_cansleep(ddata->dclk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (i < getbuf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) getbuf[i] = get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ndelay(siox_bytegap_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) gpiod_set_value_cansleep(ddata->dld, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ndelay(siox_loadhigh_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) gpiod_set_value_cansleep(ddata->dld, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * Resetting dout isn't necessary protocol wise, but it makes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * signals more pretty because the dout level is deterministic between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * cycles. Note that this only affects dout between the master and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * first siox device. dout for the later devices depend on the output of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * the previous siox device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) gpiod_set_value_cansleep(ddata->dout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int siox_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct siox_gpio_ddata *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct siox_master *smaster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) smaster = siox_master_alloc(&pdev->dev, sizeof(*ddata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!smaster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dev_err(dev, "failed to allocate siox master\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) platform_set_drvdata(pdev, smaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ddata = siox_master_get_devdata(smaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ddata->din = devm_gpiod_get(dev, "din", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (IS_ERR(ddata->din)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = PTR_ERR(ddata->din);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_err(dev, "Failed to get %s GPIO: %d\n", "din", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ddata->dout = devm_gpiod_get(dev, "dout", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (IS_ERR(ddata->dout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = PTR_ERR(ddata->dout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dev_err(dev, "Failed to get %s GPIO: %d\n", "dout", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ddata->dclk = devm_gpiod_get(dev, "dclk", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (IS_ERR(ddata->dclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ret = PTR_ERR(ddata->dclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) dev_err(dev, "Failed to get %s GPIO: %d\n", "dclk", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ddata->dld = devm_gpiod_get(dev, "dld", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (IS_ERR(ddata->dld)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = PTR_ERR(ddata->dld);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dev_err(dev, "Failed to get %s GPIO: %d\n", "dld", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) smaster->pushpull = siox_gpio_pushpull;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* XXX: determine automatically like spi does */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) smaster->busno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = siox_master_register(smaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_err(dev, "Failed to register siox master: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) siox_master_put(smaster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int siox_gpio_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct siox_master *master = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) siox_master_unregister(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static const struct of_device_id siox_gpio_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) { .compatible = "eckelmann,siox-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_DEVICE_TABLE(of, siox_gpio_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct platform_driver siox_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .probe = siox_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .remove = siox_gpio_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .name = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .of_match_table = siox_gpio_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) module_platform_driver(siox_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) MODULE_ALIAS("platform:" DRIVER_NAME);