Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  4 *	Andrew F. Davis <afd@ti.com>
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include <linux/gpio/driver.h>
  8#include <linux/i2c.h>
  9#include <linux/module.h>
 10#include <linux/mutex.h>
 11
 12#define TPIC2810_WS_COMMAND 0x44
 13
 14/**
 15 * struct tpic2810 - GPIO driver data
 16 * @chip: GPIO controller chip
 17 * @client: I2C device pointer
 18 * @buffer: Buffer for device register
 19 * @lock: Protects write sequences
 20 */
 21struct tpic2810 {
 22	struct gpio_chip chip;
 23	struct i2c_client *client;
 24	u8 buffer;
 25	struct mutex lock;
 26};
 27
 28static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
 29
 30static int tpic2810_get_direction(struct gpio_chip *chip,
 31				  unsigned offset)
 32{
 33	/* This device always output */
 34	return GPIO_LINE_DIRECTION_OUT;
 35}
 36
 37static int tpic2810_direction_input(struct gpio_chip *chip,
 38				    unsigned offset)
 39{
 40	/* This device is output only */
 41	return -EINVAL;
 42}
 43
 44static int tpic2810_direction_output(struct gpio_chip *chip,
 45				     unsigned offset, int value)
 46{
 47	/* This device always output */
 48	tpic2810_set(chip, offset, value);
 49	return 0;
 50}
 51
 52static void tpic2810_set_mask_bits(struct gpio_chip *chip, u8 mask, u8 bits)
 53{
 54	struct tpic2810 *gpio = gpiochip_get_data(chip);
 55	u8 buffer;
 56	int err;
 57
 58	mutex_lock(&gpio->lock);
 59
 60	buffer = gpio->buffer & ~mask;
 61	buffer |= (mask & bits);
 
 
 62
 63	err = i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
 64					buffer);
 65	if (!err)
 66		gpio->buffer = buffer;
 67
 68	mutex_unlock(&gpio->lock);
 69}
 70
 71static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
 72{
 73	tpic2810_set_mask_bits(chip, BIT(offset), value ? BIT(offset) : 0);
 74}
 75
 76static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
 77				  unsigned long *bits)
 78{
 79	tpic2810_set_mask_bits(chip, *mask, *bits);
 
 
 
 
 
 
 
 
 
 
 
 
 80}
 81
 82static const struct gpio_chip template_chip = {
 83	.label			= "tpic2810",
 84	.owner			= THIS_MODULE,
 85	.get_direction		= tpic2810_get_direction,
 86	.direction_input	= tpic2810_direction_input,
 87	.direction_output	= tpic2810_direction_output,
 88	.set			= tpic2810_set,
 89	.set_multiple		= tpic2810_set_multiple,
 90	.base			= -1,
 91	.ngpio			= 8,
 92	.can_sleep		= true,
 93};
 94
 95static const struct of_device_id tpic2810_of_match_table[] = {
 96	{ .compatible = "ti,tpic2810" },
 97	{ /* sentinel */ }
 98};
 99MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
100
101static int tpic2810_probe(struct i2c_client *client)
 
102{
103	struct tpic2810 *gpio;
104	int ret;
105
106	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
107	if (!gpio)
108		return -ENOMEM;
109
110	i2c_set_clientdata(client, gpio);
111
112	gpio->chip = template_chip;
113	gpio->chip.parent = &client->dev;
114
115	gpio->client = client;
116
117	mutex_init(&gpio->lock);
118
119	ret = gpiochip_add_data(&gpio->chip, gpio);
120	if (ret < 0) {
121		dev_err(&client->dev, "Unable to register gpiochip\n");
122		return ret;
123	}
124
125	return 0;
126}
127
128static void tpic2810_remove(struct i2c_client *client)
129{
130	struct tpic2810 *gpio = i2c_get_clientdata(client);
131
132	gpiochip_remove(&gpio->chip);
 
 
133}
134
135static const struct i2c_device_id tpic2810_id_table[] = {
136	{ "tpic2810", },
137	{ /* sentinel */ }
138};
139MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
140
141static struct i2c_driver tpic2810_driver = {
142	.driver = {
143		.name = "tpic2810",
144		.of_match_table = tpic2810_of_match_table,
145	},
146	.probe_new = tpic2810_probe,
147	.remove = tpic2810_remove,
148	.id_table = tpic2810_id_table,
149};
150module_i2c_driver(tpic2810_driver);
151
152MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
153MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
154MODULE_LICENSE("GPL v2");
v4.6
 
  1/*
  2 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  3 *	Andrew F. Davis <afd@ti.com>
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License version 2 as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 10 * kind, whether expressed or implied; without even the implied warranty
 11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License version 2 for more details.
 13 */
 14
 15#include <linux/gpio/driver.h>
 16#include <linux/i2c.h>
 17#include <linux/module.h>
 18#include <linux/mutex.h>
 19
 20#define TPIC2810_WS_COMMAND 0x44
 21
 22/**
 23 * struct tpic2810 - GPIO driver data
 24 * @chip: GPIO controller chip
 25 * @client: I2C device pointer
 26 * @buffer: Buffer for device register
 27 * @lock: Protects write sequences
 28 */
 29struct tpic2810 {
 30	struct gpio_chip chip;
 31	struct i2c_client *client;
 32	u8 buffer;
 33	struct mutex lock;
 34};
 35
 36static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value);
 37
 38static int tpic2810_get_direction(struct gpio_chip *chip,
 39				  unsigned offset)
 40{
 41	/* This device always output */
 42	return 0;
 43}
 44
 45static int tpic2810_direction_input(struct gpio_chip *chip,
 46				    unsigned offset)
 47{
 48	/* This device is output only */
 49	return -EINVAL;
 50}
 51
 52static int tpic2810_direction_output(struct gpio_chip *chip,
 53				     unsigned offset, int value)
 54{
 55	/* This device always output */
 56	tpic2810_set(chip, offset, value);
 57	return 0;
 58}
 59
 60static void tpic2810_set(struct gpio_chip *chip, unsigned offset, int value)
 61{
 62	struct tpic2810 *gpio = gpiochip_get_data(chip);
 
 
 63
 64	mutex_lock(&gpio->lock);
 65
 66	if (value)
 67		gpio->buffer |= BIT(offset);
 68	else
 69		gpio->buffer &= ~BIT(offset);
 70
 71	i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
 72				  gpio->buffer);
 
 
 73
 74	mutex_unlock(&gpio->lock);
 75}
 76
 
 
 
 
 
 77static void tpic2810_set_multiple(struct gpio_chip *chip, unsigned long *mask,
 78				  unsigned long *bits)
 79{
 80	struct tpic2810 *gpio = gpiochip_get_data(chip);
 81
 82	mutex_lock(&gpio->lock);
 83
 84	/* clear bits under mask */
 85	gpio->buffer &= ~(*mask);
 86	/* set bits under mask */
 87	gpio->buffer |= ((*mask) & (*bits));
 88
 89	i2c_smbus_write_byte_data(gpio->client, TPIC2810_WS_COMMAND,
 90				  gpio->buffer);
 91
 92	mutex_unlock(&gpio->lock);
 93}
 94
 95static struct gpio_chip template_chip = {
 96	.label			= "tpic2810",
 97	.owner			= THIS_MODULE,
 98	.get_direction		= tpic2810_get_direction,
 99	.direction_input	= tpic2810_direction_input,
100	.direction_output	= tpic2810_direction_output,
101	.set			= tpic2810_set,
102	.set_multiple		= tpic2810_set_multiple,
103	.base			= -1,
104	.ngpio			= 8,
105	.can_sleep		= true,
106};
107
108static const struct of_device_id tpic2810_of_match_table[] = {
109	{ .compatible = "ti,tpic2810" },
110	{ /* sentinel */ }
111};
112MODULE_DEVICE_TABLE(of, tpic2810_of_match_table);
113
114static int tpic2810_probe(struct i2c_client *client,
115			  const struct i2c_device_id *id)
116{
117	struct tpic2810 *gpio;
118	int ret;
119
120	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
121	if (!gpio)
122		return -ENOMEM;
123
124	i2c_set_clientdata(client, gpio);
125
126	gpio->chip = template_chip;
127	gpio->chip.parent = &client->dev;
128
129	gpio->client = client;
130
131	mutex_init(&gpio->lock);
132
133	ret = gpiochip_add_data(&gpio->chip, gpio);
134	if (ret < 0) {
135		dev_err(&client->dev, "Unable to register gpiochip\n");
136		return ret;
137	}
138
139	return 0;
140}
141
142static int tpic2810_remove(struct i2c_client *client)
143{
144	struct tpic2810 *gpio = i2c_get_clientdata(client);
145
146	gpiochip_remove(&gpio->chip);
147
148	return 0;
149}
150
151static const struct i2c_device_id tpic2810_id_table[] = {
152	{ "tpic2810", },
153	{ /* sentinel */ }
154};
155MODULE_DEVICE_TABLE(i2c, tpic2810_id_table);
156
157static struct i2c_driver tpic2810_driver = {
158	.driver = {
159		.name = "tpic2810",
160		.of_match_table = tpic2810_of_match_table,
161	},
162	.probe = tpic2810_probe,
163	.remove = tpic2810_remove,
164	.id_table = tpic2810_id_table,
165};
166module_i2c_driver(tpic2810_driver);
167
168MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
169MODULE_DESCRIPTION("TPIC2810 8-Bit LED Driver GPIO Driver");
170MODULE_LICENSE("GPL v2");