spi-master.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**
 * @note Copyright (C) 2016 Philippe Gerum <rpm@xenomai.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/spi/spi.h>
25
#include <linux/gpio.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include "spi-master.h"

static inline
struct device *to_kdev(struct rtdm_spi_remote_slave *slave)
{
	return rtdm_dev_to_kdev(&slave->dev);
}

static inline struct rtdm_spi_remote_slave *fd_to_slave(struct rtdm_fd *fd)
{
	struct rtdm_device *dev = rtdm_fd_device(fd);

	return container_of(dev, struct rtdm_spi_remote_slave, dev);
}

static int update_slave_config(struct rtdm_spi_remote_slave *slave,
			       struct rtdm_spi_config *config)
{
	struct rtdm_spi_config old_config;
	struct rtdm_spi_master *master = slave->master;
	int ret;

	rtdm_mutex_lock(&master->bus_lock);

	old_config = slave->config;
	slave->config = *config;
	ret = slave->master->ops->configure(slave);
	if (ret) {
		slave->config = old_config;
		rtdm_mutex_unlock(&master->bus_lock);
		return ret;
	}

	rtdm_mutex_unlock(&master->bus_lock);
	
	dev_info(to_kdev(slave),
		 "configured mode %d, %s%s%s%s%u bits/w, %u Hz max\n",
		 (int) (slave->config.mode & (SPI_CPOL | SPI_CPHA)),
		 (slave->config.mode & SPI_CS_HIGH) ? "cs_high, " : "",
		 (slave->config.mode & SPI_LSB_FIRST) ? "lsb, " : "",
		 (slave->config.mode & SPI_3WIRE) ? "3wire, " : "",
		 (slave->config.mode & SPI_LOOP) ? "loopback, " : "",
		 slave->config.bits_per_word,
		 slave->config.speed_hz);
	
	return 0;
}

static int spi_master_open(struct rtdm_fd *fd, int oflags)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;

	if (master->ops->open)
		return master->ops->open(slave);
		
	return 0;
}

static void spi_master_close(struct rtdm_fd *fd)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;
	rtdm_lockctx_t c;

	rtdm_lock_get_irqsave(&master->lock, c);

	if (master->cs == slave)
		master->cs = NULL;

	rtdm_lock_put_irqrestore(&master->lock, c);

	if (master->ops->close)
		master->ops->close(slave);
}

static int do_chip_select(struct rtdm_spi_remote_slave *slave)
{				/* master->bus_lock held */
	struct rtdm_spi_master *master = slave->master;
	rtdm_lockctx_t c;
106
	int state;
107 108 109 110 111 112 113 114

	if (slave->config.speed_hz == 0)
		return -EINVAL; /* Setup is missing. */

	/* Serialize with spi_master_close() */
	rtdm_lock_get_irqsave(&master->lock, c);
	
	if (master->cs != slave) {
115 116 117 118 119
		if (gpio_is_valid(slave->cs_gpio)) {
			state = !!(slave->config.mode & SPI_CS_HIGH);
			gpiod_set_raw_value(slave->cs_gpiod, state);
		} else
			master->ops->chip_select(slave, true);
120 121 122 123 124 125 126 127 128 129 130 131
		master->cs = slave;
	}

	rtdm_lock_put_irqrestore(&master->lock, c);

	return 0;
}

static void do_chip_deselect(struct rtdm_spi_remote_slave *slave)
{				/* master->bus_lock held */
	struct rtdm_spi_master *master = slave->master;
	rtdm_lockctx_t c;
132
	int state;
133 134

	rtdm_lock_get_irqsave(&master->lock, c);
135 136 137 138 139 140 141

	if (gpio_is_valid(slave->cs_gpio)) {
		state = !(slave->config.mode & SPI_CS_HIGH);
		gpiod_set_raw_value(slave->cs_gpiod, state);
	} else
		master->ops->chip_select(slave, false);

142
	master->cs = NULL;
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	rtdm_lock_put_irqrestore(&master->lock, c);
}

static int spi_master_ioctl_rt(struct rtdm_fd *fd,
			       unsigned int request, void *arg)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;
	struct rtdm_spi_config config;
	int ret;

	switch (request) {
	case SPI_RTIOC_SET_CONFIG:
		ret = rtdm_safe_copy_from_user(fd, &config,
					       arg, sizeof(config));
		if (ret == 0)
			ret = update_slave_config(slave, &config);
		break;
	case SPI_RTIOC_GET_CONFIG:
		rtdm_mutex_lock(&master->bus_lock);
		config = slave->config;
		rtdm_mutex_unlock(&master->bus_lock);
		ret = rtdm_safe_copy_to_user(fd, arg,
					     &config, sizeof(config));
		break;
	case SPI_RTIOC_TRANSFER:
		ret = -EINVAL;
		if (master->ops->transfer_iobufs) {
			rtdm_mutex_lock(&master->bus_lock);
			ret = do_chip_select(slave);
			if (ret == 0) {
				ret = master->ops->transfer_iobufs(slave);
				do_chip_deselect(slave);
			}
			rtdm_mutex_unlock(&master->bus_lock);
		}
		break;
	default:
		ret = -ENOSYS;
	}

	return ret;
}

static int spi_master_ioctl_nrt(struct rtdm_fd *fd,
				unsigned int request, void *arg)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;
	struct rtdm_spi_iobufs iobufs;
	int ret;

	switch (request) {
	case SPI_RTIOC_SET_IOBUFS:
		ret = rtdm_safe_copy_from_user(fd, &iobufs,
					       arg, sizeof(iobufs));
		if (ret)
			break;
		/*
		 * No transfer can happen without I/O buffers being
		 * set, and I/O buffers cannot be reset, therefore we
		 * need no serialization with the transfer code here.
		 */
		mutex_lock(&slave->ctl_lock);
		ret = master->ops->set_iobufs(slave, &iobufs);
		mutex_unlock(&slave->ctl_lock);
		if (ret == 0)
			ret = rtdm_safe_copy_to_user(fd, arg,
					     &iobufs, sizeof(iobufs));
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

static ssize_t spi_master_read_rt(struct rtdm_fd *fd,
				  void __user *u_buf, size_t len)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;
	void *rx;
	int ret;

	if (len == 0)
		return 0;

	rx = xnmalloc(len);
	if (rx == NULL)
		return -ENOMEM;

	rtdm_mutex_lock(&master->bus_lock);
	ret = do_chip_select(slave);
	if (ret == 0) {
		ret = master->ops->read(slave, rx, len);
		do_chip_deselect(slave);
	}
	rtdm_mutex_unlock(&master->bus_lock);
243 244
	if (ret > 0)
		ret = rtdm_safe_copy_to_user(fd, u_buf, rx, ret);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	
	xnfree(rx);
	
	return ret;
}

static ssize_t spi_master_write_rt(struct rtdm_fd *fd,
				   const void __user *u_buf, size_t len)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	struct rtdm_spi_master *master = slave->master;
	void *tx;
	int ret;

	if (len == 0)
		return 0;

	tx = xnmalloc(len);
	if (tx == NULL)
		return -ENOMEM;

	ret = rtdm_safe_copy_from_user(fd, tx, u_buf, len);
	if (ret == 0) {
		rtdm_mutex_lock(&master->bus_lock);
		ret = do_chip_select(slave);
		if (ret == 0) {
			ret = master->ops->write(slave, tx, len);
			do_chip_deselect(slave);
		}
		rtdm_mutex_unlock(&master->bus_lock);
	}
	
	xnfree(tx);

	return ret;
}

static void iobufs_vmopen(struct vm_area_struct *vma)
{
	struct rtdm_spi_remote_slave *slave = vma->vm_private_data;

	atomic_inc(&slave->mmap_refs);
	dev_dbg(slave_to_kdev(slave), "mapping added\n");
}

static void iobufs_vmclose(struct vm_area_struct *vma)
{
	struct rtdm_spi_remote_slave *slave = vma->vm_private_data;

	if (atomic_dec_and_test(&slave->mmap_refs)) {
		slave->master->ops->mmap_release(slave);
		dev_dbg(slave_to_kdev(slave), "mapping released\n");
	}
}

static struct vm_operations_struct iobufs_vmops = {
	.open = iobufs_vmopen,
	.close = iobufs_vmclose,
};

static int spi_master_mmap(struct rtdm_fd *fd, struct vm_area_struct *vma)
{
	struct rtdm_spi_remote_slave *slave = fd_to_slave(fd);
	int ret;

	if (slave->master->ops->mmap_iobufs == NULL)
		return -EINVAL;

	ret = slave->master->ops->mmap_iobufs(slave, vma);
	if (ret)
		return ret;

	dev_dbg(slave_to_kdev(slave), "mapping created\n");
	atomic_inc(&slave->mmap_refs);

	if (slave->master->ops->mmap_release) {
		vma->vm_ops = &iobufs_vmops;
		vma->vm_private_data = slave;
	}

	return 0;
}

static char *spi_slave_devnode(struct device *dev, umode_t *mode)
{
	return kasprintf(GFP_KERNEL, "rtdm/%s/%s",
			 dev->class->name,
			 dev_name(dev));
}

struct rtdm_spi_master *
__rtdm_spi_alloc_master(struct device *dev, size_t size, int off)
{
	struct rtdm_spi_master *master;
	struct spi_master *kmaster;

	kmaster = spi_alloc_master(dev, size);
	if (kmaster == NULL)
		return NULL;
	
	master = (void *)(kmaster + 1) + off;
	master->kmaster = kmaster;
	spi_master_set_devdata(kmaster, master);

	return master;
}
EXPORT_SYMBOL_GPL(__rtdm_spi_alloc_master);

int __rtdm_spi_setup_driver(struct rtdm_spi_master *master)
{
	master->classname = kstrdup(
		dev_name(&master->kmaster->dev), GFP_KERNEL);
	master->devclass = class_create(THIS_MODULE,
		master->classname);
	if (IS_ERR(master->devclass)) {
		kfree(master->classname);
		printk(XENO_ERR "cannot create sysfs class\n");
		return PTR_ERR(master->devclass);
	}

	master->devclass->devnode = spi_slave_devnode;
	master->cs = NULL;

	master->driver.profile_info = (struct rtdm_profile_info)
		RTDM_PROFILE_INFO(rtdm_spi_master,
				  RTDM_CLASS_SPI,
				  master->subclass,
				  0);
	master->driver.device_flags = RTDM_NAMED_DEVICE;
	master->driver.base_minor = 0;
	master->driver.device_count = 256;
	master->driver.context_size = 0;
	master->driver.ops = (struct rtdm_fd_ops){
		.open		=	spi_master_open,
		.close		=	spi_master_close,
		.read_rt	=	spi_master_read_rt,
		.write_rt	=	spi_master_write_rt,
		.ioctl_rt	=	spi_master_ioctl_rt,
		.ioctl_nrt	=	spi_master_ioctl_nrt,
		.mmap		=	spi_master_mmap,
	};
	
	rtdm_drv_set_sysclass(&master->driver, master->devclass);

	INIT_LIST_HEAD(&master->slaves);
	rtdm_lock_init(&master->lock);
	rtdm_mutex_init(&master->bus_lock);

	return 0;
}

static int spi_transfer_one_unimp(struct spi_master *master,
				  struct spi_device *spi,
				  struct spi_transfer *tfr)
{
	return -ENODEV;
}

int rtdm_spi_add_master(struct rtdm_spi_master *master)
{
	struct spi_master *kmaster = master->kmaster;

	/*
	 * Prevent the transfer handler to be called from the regular
	 * SPI stack, just in case.
	 */
	kmaster->transfer_one = spi_transfer_one_unimp;
	master->devclass = NULL;

	/*
	 * Add the core SPI driver, devices on the bus will be
	 * enumerated, handed to spi_device_probe().
	 */
	return spi_register_master(kmaster);
}
EXPORT_SYMBOL_GPL(rtdm_spi_add_master);

void rtdm_spi_remove_master(struct rtdm_spi_master *master)
{
	struct class *class = master->devclass;
	char *classname = master->classname;
	
	rtdm_mutex_destroy(&master->bus_lock);
	spi_unregister_master(master->kmaster);
429
	rtdm_drv_set_sysclass(&master->driver, NULL);
430 431 432 433 434 435
	class_destroy(class);
	kfree(classname);
}
EXPORT_SYMBOL_GPL(rtdm_spi_remove_master);

MODULE_LICENSE("GPL");