Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * CBUS I2C driver for Nokia Internet Tablets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2004-2010 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Public License. See the file "COPYING" in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * Bit counts are derived from Nokia implementation. These should be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * if other CBUS implementations appear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define CBUS_ADDR_BITS	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CBUS_REG_BITS	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct cbus_host {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	spinlock_t	lock;		/* host lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct device	*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct gpio_desc *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct gpio_desc *dat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct gpio_desc *sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * cbus_send_bit - sends one bit over the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * @host: the host we're using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @bit: one bit of information to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void cbus_send_bit(struct cbus_host *host, unsigned bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	gpiod_set_value(host->dat, bit ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	gpiod_set_value(host->clk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	gpiod_set_value(host->clk, 0);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * cbus_send_data - sends @len amount of data over the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @host: the host we're using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * @data: the data to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * @len: size of the transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	for (i = len; i > 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		cbus_send_bit(host, data & (1 << (i - 1)));
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * cbus_receive_bit - receives one bit from the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * @host: the host we're using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static int cbus_receive_bit(struct cbus_host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	gpiod_set_value(host->clk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	ret = gpiod_get_value(host->dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	gpiod_set_value(host->clk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * cbus_receive_word - receives 16-bit word from the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * @host: the host we're using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int cbus_receive_word(struct cbus_host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	for (i = 16; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		int bit = cbus_receive_bit(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		if (bit < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			return bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		if (bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			ret |= 1 << (i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * cbus_transfer - transfers data over the bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * @host: the host we're using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * @rw: read/write flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * @dev: device address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * @reg: register address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			 unsigned reg, unsigned data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/* We don't want interrupts disturbing our transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	spin_lock_irqsave(&host->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Reset state and start of transfer, SEL stays down during transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	gpiod_set_value(host->sel, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Set the DAT pin to output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	gpiod_direction_output(host->dat, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	/* Send the device address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	cbus_send_data(host, dev, CBUS_ADDR_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	/* Send the rw flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	cbus_send_bit(host, rw == I2C_SMBUS_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	/* Send the register address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	cbus_send_data(host, reg, CBUS_REG_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (rw == I2C_SMBUS_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		cbus_send_data(host, data, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ret = gpiod_direction_input(host->dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			dev_dbg(host->dev, "failed setting direction\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		gpiod_set_value(host->clk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		ret = cbus_receive_word(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			dev_dbg(host->dev, "failed receiving data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* Indicate end of transfer, SEL goes up until next transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	gpiod_set_value(host->sel, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	gpiod_set_value(host->clk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	gpiod_set_value(host->clk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	spin_unlock_irqrestore(&host->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int cbus_i2c_smbus_xfer(struct i2c_adapter	*adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			       u16			addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			       unsigned short		flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			       char			read_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			       u8			command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			       int			size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			       union i2c_smbus_data	*data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct cbus_host *chost = i2c_get_adapdata(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (size != I2C_SMBUS_WORD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			    command, data->word);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (read_write == I2C_SMBUS_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		data->word = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static u32 cbus_i2c_func(struct i2c_adapter *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static const struct i2c_algorithm cbus_i2c_algo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.smbus_xfer		= cbus_i2c_smbus_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.smbus_xfer_atomic	= cbus_i2c_smbus_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.functionality		= cbus_i2c_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int cbus_i2c_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct i2c_adapter *adapter = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	i2c_del_adapter(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int cbus_i2c_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct i2c_adapter *adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct cbus_host *chost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (!adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	chost = devm_kzalloc(&pdev->dev, sizeof(*chost), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (!chost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (gpiod_count(&pdev->dev, NULL) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	chost->clk = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (IS_ERR(chost->clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return PTR_ERR(chost->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	chost->dat = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (IS_ERR(chost->dat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return PTR_ERR(chost->dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	chost->sel = devm_gpiod_get_index(&pdev->dev, NULL, 2, GPIOD_OUT_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (IS_ERR(chost->sel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return PTR_ERR(chost->sel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	gpiod_set_consumer_name(chost->clk, "CBUS clk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	gpiod_set_consumer_name(chost->dat, "CBUS dat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	gpiod_set_consumer_name(chost->sel, "CBUS sel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	adapter->owner		= THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	adapter->class		= I2C_CLASS_HWMON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	adapter->dev.parent	= &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	adapter->dev.of_node	= pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	adapter->nr		= pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	adapter->timeout	= HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	adapter->algo		= &cbus_i2c_algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	spin_lock_init(&chost->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	chost->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	i2c_set_adapdata(adapter, chost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	platform_set_drvdata(pdev, adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return i2c_add_numbered_adapter(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #if defined(CONFIG_OF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static const struct of_device_id i2c_cbus_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	{ .compatible = "i2c-cbus-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static struct platform_driver cbus_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.probe	= cbus_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.remove	= cbus_i2c_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		.name	= "i2c-cbus-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		.of_match_table = of_match_ptr(i2c_cbus_dt_ids),
^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) module_platform_driver(cbus_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) MODULE_ALIAS("platform:i2c-cbus-gpio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) MODULE_DESCRIPTION("CBUS I2C driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) MODULE_AUTHOR("Juha Yrjölä");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) MODULE_AUTHOR("David Weinehall");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) MODULE_AUTHOR("Mikko Ylinen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) MODULE_AUTHOR("Felipe Balbi");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) MODULE_LICENSE("GPL");