blob: f60b265b4da15a949701fac80c7de4927e0a49fd [file] [log] [blame]
Andrzej Hajdacac47f12012-11-22 11:39:18 -03001/*
2 * Samsung LSI S5C73M3 8M pixel camera driver
3 *
4 * Copyright (C) 2012, Samsung Electronics, Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
6 * Andrzej Hajda <a.hajda@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/sizes.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/media.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/spi/spi.h>
25
26#include "s5c73m3.h"
27
28#define S5C73M3_SPI_DRV_NAME "S5C73M3-SPI"
29
Sylwester Nawrockibce67442013-12-20 19:46:44 -030030static const struct of_device_id s5c73m3_spi_ids[] = {
31 { .compatible = "samsung,s5c73m3" },
32 { }
33};
34
Andrzej Hajdacac47f12012-11-22 11:39:18 -030035enum spi_direction {
36 SPI_DIR_RX,
37 SPI_DIR_TX
38};
39
40static int spi_xmit(struct spi_device *spi_dev, void *addr, const int len,
41 enum spi_direction dir)
42{
43 struct spi_message msg;
44 int r;
45 struct spi_transfer xfer = {
46 .len = len,
47 };
48
49 if (dir == SPI_DIR_TX)
50 xfer.tx_buf = addr;
51 else
52 xfer.rx_buf = addr;
53
54 if (spi_dev == NULL) {
55 dev_err(&spi_dev->dev, "SPI device is uninitialized\n");
56 return -ENODEV;
57 }
58
59 spi_message_init(&msg);
60 spi_message_add_tail(&xfer, &msg);
61
62 r = spi_sync(spi_dev, &msg);
63 if (r < 0)
64 dev_err(&spi_dev->dev, "%s spi_sync failed %d\n", __func__, r);
65
66 return r;
67}
68
69int s5c73m3_spi_write(struct s5c73m3 *state, const void *addr,
70 const unsigned int len, const unsigned int tx_size)
71{
72 struct spi_device *spi_dev = state->spi_dev;
73 u32 count = len / tx_size;
74 u32 extra = len % tx_size;
75 unsigned int i, j = 0;
76 u8 padding[32];
77 int r = 0;
78
79 memset(padding, 0, sizeof(padding));
80
Sachin Kamat8638a462013-04-30 05:04:09 -030081 for (i = 0; i < count; i++) {
Andrzej Hajdacac47f12012-11-22 11:39:18 -030082 r = spi_xmit(spi_dev, (void *)addr + j, tx_size, SPI_DIR_TX);
83 if (r < 0)
84 return r;
85 j += tx_size;
86 }
87
88 if (extra > 0) {
89 r = spi_xmit(spi_dev, (void *)addr + j, extra, SPI_DIR_TX);
90 if (r < 0)
91 return r;
92 }
93
94 return spi_xmit(spi_dev, padding, sizeof(padding), SPI_DIR_TX);
95}
96
97int s5c73m3_spi_read(struct s5c73m3 *state, void *addr,
98 const unsigned int len, const unsigned int tx_size)
99{
100 struct spi_device *spi_dev = state->spi_dev;
101 u32 count = len / tx_size;
102 u32 extra = len % tx_size;
103 unsigned int i, j = 0;
104 int r = 0;
105
Sachin Kamat8638a462013-04-30 05:04:09 -0300106 for (i = 0; i < count; i++) {
Andrzej Hajdacac47f12012-11-22 11:39:18 -0300107 r = spi_xmit(spi_dev, addr + j, tx_size, SPI_DIR_RX);
108 if (r < 0)
109 return r;
110 j += tx_size;
111 }
112
113 if (extra > 0)
114 return spi_xmit(spi_dev, addr + j, extra, SPI_DIR_RX);
115
116 return 0;
117}
118
Sylwester Nawrockic2668c02013-02-06 17:35:41 -0300119static int s5c73m3_spi_probe(struct spi_device *spi)
Andrzej Hajdacac47f12012-11-22 11:39:18 -0300120{
121 int r;
122 struct s5c73m3 *state = container_of(spi->dev.driver, struct s5c73m3,
123 spidrv.driver);
124 spi->bits_per_word = 32;
125
126 r = spi_setup(spi);
127 if (r < 0) {
128 dev_err(&spi->dev, "spi_setup() failed\n");
129 return r;
130 }
131
132 mutex_lock(&state->lock);
133 state->spi_dev = spi;
134 mutex_unlock(&state->lock);
135
136 v4l2_info(&state->sensor_sd, "S5C73M3 SPI probed successfully\n");
137 return 0;
138}
139
Sylwester Nawrockic2668c02013-02-06 17:35:41 -0300140static int s5c73m3_spi_remove(struct spi_device *spi)
Andrzej Hajdacac47f12012-11-22 11:39:18 -0300141{
142 return 0;
143}
144
145int s5c73m3_register_spi_driver(struct s5c73m3 *state)
146{
147 struct spi_driver *spidrv = &state->spidrv;
148
Sylwester Nawrockic2668c02013-02-06 17:35:41 -0300149 spidrv->remove = s5c73m3_spi_remove;
Andrzej Hajdacac47f12012-11-22 11:39:18 -0300150 spidrv->probe = s5c73m3_spi_probe;
151 spidrv->driver.name = S5C73M3_SPI_DRV_NAME;
152 spidrv->driver.bus = &spi_bus_type;
153 spidrv->driver.owner = THIS_MODULE;
Sylwester Nawrockibce67442013-12-20 19:46:44 -0300154 spidrv->driver.of_match_table = s5c73m3_spi_ids;
Andrzej Hajdacac47f12012-11-22 11:39:18 -0300155
156 return spi_register_driver(spidrv);
157}
158
159void s5c73m3_unregister_spi_driver(struct s5c73m3 *state)
160{
161 spi_unregister_driver(&state->spidrv);
162}