2323#include <linux/iio/sysfs.h>
2424#include <linux/iio/events.h>
2525#include <linux/iio/buffer.h>
26- #include "../ring_hw.h"
26+ #include <linux/iio/kfifo_buf.h>
2727
2828#define SCA3000_WRITE_REG (a ) (((a) << 2) | 0x02)
2929#define SCA3000_READ_REG (a ) ((a) << 2)
@@ -173,7 +173,7 @@ struct sca3000_state {
173173 struct mutex lock ;
174174 int bpse ;
175175 /* Can these share a cacheline ? */
176- u8 rx [2 ] ____cacheline_aligned ;
176+ u8 rx [384 ] ____cacheline_aligned ;
177177 u8 tx [6 ] ____cacheline_aligned ;
178178};
179179
@@ -572,6 +572,10 @@ static const struct iio_event_spec sca3000_event = {
572572 .mask_separate = BIT (IIO_EV_INFO_VALUE ) | BIT (IIO_EV_INFO_ENABLE ),
573573};
574574
575+ /*
576+ * Note the hack in the number of bits to pretend we have 2 more than
577+ * we do in the fifo.
578+ */
575579#define SCA3000_CHAN (index , mod ) \
576580 { \
577581 .type = IIO_ACCEL, \
@@ -584,9 +588,10 @@ static const struct iio_event_spec sca3000_event = {
584588 .scan_index = index, \
585589 .scan_type = { \
586590 .sign = 's', \
587- .realbits = 11 , \
591+ .realbits = 13 , \
588592 .storagebits = 16, \
589- .shift = 5, \
593+ .shift = 3, \
594+ .endianness = IIO_BE, \
590595 }, \
591596 .event_spec = &sca3000_event, \
592597 .num_event_specs = 1, \
@@ -935,19 +940,71 @@ static const struct attribute_group sca3000_attribute_group = {
935940 .attrs = sca3000_attributes ,
936941};
937942
943+ static int sca3000_read_data (struct sca3000_state * st ,
944+ u8 reg_address_high ,
945+ u8 * rx ,
946+ int len )
947+ {
948+ int ret ;
949+ struct spi_transfer xfer [2 ] = {
950+ {
951+ .len = 1 ,
952+ .tx_buf = st -> tx ,
953+ }, {
954+ .len = len ,
955+ .rx_buf = rx ,
956+ }
957+ };
958+
959+ st -> tx [0 ] = SCA3000_READ_REG (reg_address_high );
960+ ret = spi_sync_transfer (st -> us , xfer , ARRAY_SIZE (xfer ));
961+ if (ret ) {
962+ dev_err (get_device (& st -> us -> dev ), "problem reading register" );
963+ return ret ;
964+ }
965+
966+ return 0 ;
967+ }
968+
938969/**
939970 * sca3000_ring_int_process() ring specific interrupt handling.
940971 *
941972 * This is only split from the main interrupt handler so as to
942973 * reduce the amount of code if the ring buffer is not enabled.
943974 **/
944- static void sca3000_ring_int_process (u8 val , struct iio_buffer * ring )
975+ static void sca3000_ring_int_process (u8 val , struct iio_dev * indio_dev )
945976{
946- if (val & (SCA3000_INT_STATUS_THREE_QUARTERS |
947- SCA3000_INT_STATUS_HALF )) {
948- ring -> stufftoread = true;
949- wake_up_interruptible (& ring -> pollq );
977+ struct sca3000_state * st = iio_priv (indio_dev );
978+ int ret , i , num_available ;
979+
980+ mutex_lock (& st -> lock );
981+ if (val & SCA3000_INT_STATUS_HALF ) {
982+ ret = sca3000_read_data_short (st , SCA3000_REG_ADDR_BUF_COUNT ,
983+ 1 );
984+ if (ret )
985+ goto error_ret ;
986+ num_available = st -> rx [0 ];
987+ /*
988+ * num_available is the total number of samples available
989+ * i.e. number of time points * number of channels.
990+ */
991+ ret = sca3000_read_data (st , SCA3000_REG_ADDR_RING_OUT , st -> rx ,
992+ num_available * 2 );
993+ if (ret )
994+ goto error_ret ;
995+ for (i = 0 ; i < num_available / 3 ; i ++ ) {
996+ /*
997+ * Dirty hack to cover for 11 bit in fifo, 13 bit
998+ * direct reading.
999+ *
1000+ * In theory the bottom two bits are undefined.
1001+ * In reality they appear to always be 0.
1002+ */
1003+ iio_push_to_buffers (indio_dev , st -> rx + i * 3 * 2 );
1004+ }
9501005 }
1006+ error_ret :
1007+ mutex_unlock (& st -> lock );
9511008}
9521009
9531010/**
@@ -978,7 +1035,7 @@ static irqreturn_t sca3000_event_handler(int irq, void *private)
9781035 if (ret )
9791036 goto done ;
9801037
981- sca3000_ring_int_process (val , indio_dev -> buffer );
1038+ sca3000_ring_int_process (val , indio_dev );
9821039
9831040 if (val & SCA3000_INT_STATUS_FREE_FALL )
9841041 iio_push_event (indio_dev ,
@@ -1209,183 +1266,23 @@ static struct attribute_group sca3000_event_attribute_group = {
12091266 .name = "events" ,
12101267};
12111268
1212- static int sca3000_read_data (struct sca3000_state * st ,
1213- u8 reg_address_high ,
1214- u8 * * rx_p ,
1215- int len )
1216- {
1217- int ret ;
1218- struct spi_transfer xfer [2 ] = {
1219- {
1220- .len = 1 ,
1221- .tx_buf = st -> tx ,
1222- }, {
1223- .len = len ,
1224- }
1225- };
1226- * rx_p = kmalloc (len , GFP_KERNEL );
1227- if (!* rx_p ) {
1228- ret = - ENOMEM ;
1229- goto error_ret ;
1230- }
1231- xfer [1 ].rx_buf = * rx_p ;
1232- st -> tx [0 ] = SCA3000_READ_REG (reg_address_high );
1233- ret = spi_sync_transfer (st -> us , xfer , ARRAY_SIZE (xfer ));
1234- if (ret ) {
1235- dev_err (get_device (& st -> us -> dev ), "problem reading register" );
1236- goto error_free_rx ;
1237- }
1238-
1239- return 0 ;
1240- error_free_rx :
1241- kfree (* rx_p );
1242- error_ret :
1243- return ret ;
1244- }
1245-
1246- /**
1247- * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring
1248- * @r: the ring
1249- * @count: number of samples to try and pull
1250- * @data: output the actual samples pulled from the hw ring
1251- *
1252- * Currently does not provide timestamps. As the hardware doesn't add them they
1253- * can only be inferred approximately from ring buffer events such as 50% full
1254- * and knowledge of when buffer was last emptied. This is left to userspace.
1255- **/
1256- static int sca3000_read_first_n_hw_rb (struct iio_buffer * r ,
1257- size_t count , char __user * buf )
1258- {
1259- struct iio_hw_buffer * hw_ring = iio_to_hw_buf (r );
1260- struct iio_dev * indio_dev = hw_ring -> private ;
1261- struct sca3000_state * st = iio_priv (indio_dev );
1262- u8 * rx ;
1263- int ret , i , num_available , num_read = 0 ;
1264- int bytes_per_sample = 1 ;
1265-
1266- if (st -> bpse == 11 )
1267- bytes_per_sample = 2 ;
1268-
1269- mutex_lock (& st -> lock );
1270- if (count % bytes_per_sample ) {
1271- ret = - EINVAL ;
1272- goto error_ret ;
1273- }
1274-
1275- ret = sca3000_read_data_short (st , SCA3000_REG_ADDR_BUF_COUNT , 1 );
1276- if (ret )
1277- goto error_ret ;
1278- num_available = st -> rx [0 ];
1279- /*
1280- * num_available is the total number of samples available
1281- * i.e. number of time points * number of channels.
1282- */
1283- if (count > num_available * bytes_per_sample )
1284- num_read = num_available * bytes_per_sample ;
1285- else
1286- num_read = count ;
1287-
1288- ret = sca3000_read_data (st ,
1289- SCA3000_REG_ADDR_RING_OUT ,
1290- & rx , num_read );
1291- if (ret )
1292- goto error_ret ;
1293-
1294- for (i = 0 ; i < num_read / sizeof (u16 ); i ++ )
1295- * (((u16 * )rx ) + i ) = be16_to_cpup ((__be16 * )rx + i );
1296-
1297- if (copy_to_user (buf , rx , num_read ))
1298- ret = - EFAULT ;
1299- kfree (rx );
1300- r -> stufftoread = 0 ;
1301- error_ret :
1302- mutex_unlock (& st -> lock );
1303-
1304- return ret ? ret : num_read ;
1305- }
1306-
1307- static size_t sca3000_ring_buf_data_available (struct iio_buffer * r )
1308- {
1309- return r -> stufftoread ? r -> watermark : 0 ;
1310- }
1311-
1312- static ssize_t sca3000_show_buffer_scale (struct device * dev ,
1313- struct device_attribute * attr ,
1314- char * buf )
1315- {
1316- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
1317- struct sca3000_state * st = iio_priv (indio_dev );
1318-
1319- return sprintf (buf , "0.%06d\n" , 4 * st -> info -> scale );
1320- }
1321-
1322- static IIO_DEVICE_ATTR (in_accel_scale ,
1323- S_IRUGO ,
1324- sca3000_show_buffer_scale ,
1325- NULL,
1326- 0 ) ;
1327-
1328- /*
1329- * Ring buffer attributes
1330- * This device is a bit unusual in that the sampling frequency and bpse
1331- * only apply to the ring buffer. At all times full rate and accuracy
1332- * is available via direct reading from registers.
1333- */
1334- static const struct attribute * sca3000_ring_attributes [] = {
1335- & iio_dev_attr_in_accel_scale .dev_attr .attr ,
1336- NULL ,
1337- };
1338-
1339- static struct iio_buffer * sca3000_rb_allocate (struct iio_dev * indio_dev )
1340- {
1341- struct iio_buffer * buf ;
1342- struct iio_hw_buffer * ring ;
1343-
1344- ring = kzalloc (sizeof (* ring ), GFP_KERNEL );
1345- if (!ring )
1346- return NULL ;
1347-
1348- ring -> private = indio_dev ;
1349- buf = & ring -> buf ;
1350- buf -> stufftoread = 0 ;
1351- buf -> length = 64 ;
1352- buf -> attrs = sca3000_ring_attributes ;
1353- iio_buffer_init (buf );
1354-
1355- return buf ;
1356- }
1357-
1358- static void sca3000_ring_release (struct iio_buffer * r )
1359- {
1360- kfree (iio_to_hw_buf (r ));
1361- }
1362-
1363- static const struct iio_buffer_access_funcs sca3000_ring_access_funcs = {
1364- .read_first_n = & sca3000_read_first_n_hw_rb ,
1365- .data_available = sca3000_ring_buf_data_available ,
1366- .release = sca3000_ring_release ,
1367-
1368- .modes = INDIO_BUFFER_HARDWARE ,
1369- };
1370-
13711269static int sca3000_configure_ring (struct iio_dev * indio_dev )
13721270{
13731271 struct iio_buffer * buffer ;
13741272
1375- buffer = sca3000_rb_allocate ( indio_dev );
1273+ buffer = iio_kfifo_allocate ( );
13761274 if (!buffer )
13771275 return - ENOMEM ;
1378- indio_dev -> modes |= INDIO_BUFFER_HARDWARE ;
13791276
1380- buffer -> access = & sca3000_ring_access_funcs ;
13811277 iio_device_attach_buffer (indio_dev , buffer );
1278+ indio_dev -> modes |= INDIO_BUFFER_SOFTWARE ;
13821279
13831280 return 0 ;
13841281}
13851282
13861283static void sca3000_unconfigure_ring (struct iio_dev * indio_dev )
13871284{
1388- iio_buffer_put (indio_dev -> buffer );
1285+ iio_kfifo_free (indio_dev -> buffer );
13891286}
13901287
13911288static inline
@@ -1425,19 +1322,6 @@ static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
14251322 int ret ;
14261323 struct sca3000_state * st = iio_priv (indio_dev );
14271324
1428- /*
1429- * Set stuff to read to indicate no data present.
1430- * Need for cases where the interrupt had fired at the
1431- * end of a cycle, but the data was never read.
1432- */
1433- indio_dev -> buffer -> stufftoread = 0 ;
1434- /*
1435- * Needed to ensure the core will actually read data
1436- * from the device rather than assuming no channels
1437- * are enabled.
1438- */
1439- indio_dev -> buffer -> bytes_per_datum = 6 ;
1440-
14411325 mutex_lock (& st -> lock );
14421326
14431327 /* Enable the 50% full interrupt */
0 commit comments