^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Support Infineon TLE62x0 driver chips
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007 Simtec Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Ben Dooks, <ben@simtec.co.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/spi/tle62x0.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define CMD_READ 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define CMD_SET 0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DIAG_NORMAL 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DIAG_OVERLOAD 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define DIAG_OPEN 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define DIAG_SHORTGND 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct tle62x0_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct spi_device *us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned int nr_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int gpio_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned char tx_buff[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned char rx_buff[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int to_gpio_num(struct device_attribute *attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static inline int tle62x0_write(struct tle62x0_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned char *buff = st->tx_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int gpio_state = st->gpio_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) buff[0] = CMD_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (st->nr_gpio == 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) buff[1] = gpio_state >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) buff[2] = gpio_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) buff[1] = gpio_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_dbg(&st->us->dev, "buff %3ph\n", buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static inline int tle62x0_read(struct tle62x0_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned char *txbuff = st->tx_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct spi_transfer xfer = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .tx_buf = txbuff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .rx_buf = st->rx_buff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .len = (st->nr_gpio * 2) / 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct spi_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) txbuff[0] = CMD_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) txbuff[1] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) txbuff[2] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) txbuff[3] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spi_message_init(&msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) spi_message_add_tail(&xfer, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return spi_sync(st->us, &msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static unsigned char *decode_fault(unsigned int fault_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) fault_code &= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) switch (fault_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) case DIAG_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return "N";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) case DIAG_OVERLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return "V";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case DIAG_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return "O";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case DIAG_SHORTGND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return "G";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return "?";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static ssize_t tle62x0_status_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct tle62x0_state *st = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) char *bp = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned char *buff = st->rx_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned long fault = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) mutex_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = tle62x0_read(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_dbg(dev, "tle62x0_read() returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) mutex_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) fault <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fault |= ((unsigned long)buff[ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev_dbg(dev, "byte %d is %02x\n", ptr, buff[ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (ptr = 0; ptr < st->nr_gpio; ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *bp++ = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return bp - buf;
^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) static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static ssize_t tle62x0_gpio_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct tle62x0_state *st = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int gpio_num = to_gpio_num(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mutex_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) value = (st->gpio_state >> gpio_num) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mutex_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return snprintf(buf, PAGE_SIZE, "%d", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static ssize_t tle62x0_gpio_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct tle62x0_state *st = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int gpio_num = to_gpio_num(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) val = simple_strtoul(buf, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (buf == endp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mutex_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) st->gpio_state |= 1 << gpio_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) st->gpio_state &= ~(1 << gpio_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) tle62x0_write(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) tle62x0_gpio_show, tle62x0_gpio_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static struct device_attribute *gpio_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) [0] = &dev_attr_gpio1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) [1] = &dev_attr_gpio2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) [2] = &dev_attr_gpio3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) [3] = &dev_attr_gpio4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) [4] = &dev_attr_gpio5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) [5] = &dev_attr_gpio6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) [6] = &dev_attr_gpio7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) [7] = &dev_attr_gpio8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) [8] = &dev_attr_gpio9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) [9] = &dev_attr_gpio10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) [10] = &dev_attr_gpio11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) [11] = &dev_attr_gpio12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) [12] = &dev_attr_gpio13,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) [13] = &dev_attr_gpio14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) [14] = &dev_attr_gpio15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) [15] = &dev_attr_gpio16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int to_gpio_num(struct device_attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (gpio_attrs[ptr] == attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int tle62x0_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct tle62x0_state *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct tle62x0_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pdata = dev_get_platdata(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (pdata == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) dev_err(&spi->dev, "no device data specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (st == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) st->us = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) st->nr_gpio = pdata->gpio_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) st->gpio_state = pdata->init_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) mutex_init(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ret = device_create_file(&spi->dev, &dev_attr_status_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) dev_err(&spi->dev, "cannot create status attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto err_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_err(&spi->dev, "cannot create gpio attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto err_gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* tle62x0_write(st); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) spi_set_drvdata(spi, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) err_gpios:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) while (--ptr >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) device_remove_file(&spi->dev, gpio_attrs[ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) device_remove_file(&spi->dev, &dev_attr_status_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) err_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) kfree(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int tle62x0_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct tle62x0_state *st = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) for (ptr = 0; ptr < st->nr_gpio; ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) device_remove_file(&spi->dev, gpio_attrs[ptr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) device_remove_file(&spi->dev, &dev_attr_status_show);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) kfree(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static struct spi_driver tle62x0_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .name = "tle62x0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .probe = tle62x0_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .remove = tle62x0_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) module_spi_driver(tle62x0_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) MODULE_DESCRIPTION("TLE62x0 SPI driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_ALIAS("spi:tle62x0");