PageRenderTime 167ms CodeModel.GetById 52ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/staging/iio/iio.h

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C Header | 423 lines | 222 code | 46 blank | 155 comment | 1 complexity | fbd7ac58b67e23072a5431672f30c5a5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #ifndef _INDUSTRIAL_IO_H_
  10. #define _INDUSTRIAL_IO_H_
  11. #include <linux/device.h>
  12. #include <linux/cdev.h>
  13. #include <linux/irq.h>
  14. #include "sysfs.h"
  15. #include "chrdev.h"
  16. /* IIO TODO LIST */
  17. /*
  18. * Provide means of adjusting timer accuracy.
  19. * Currently assumes nano seconds.
  20. */
  21. /* Event interface flags */
  22. #define IIO_BUSY_BIT_POS 1
  23. /* naughty temporary hack to match these against the event version
  24. - need to flattern these together */
  25. enum iio_chan_type {
  26. /* real channel types */
  27. IIO_IN,
  28. IIO_CURRENT,
  29. IIO_POWER,
  30. IIO_ACCEL,
  31. IIO_IN_DIFF,
  32. IIO_GYRO,
  33. IIO_MAGN,
  34. IIO_LIGHT,
  35. IIO_INTENSITY,
  36. IIO_PROXIMITY,
  37. IIO_TEMP,
  38. IIO_INCLI,
  39. IIO_ROT,
  40. IIO_ANGL,
  41. IIO_TIMESTAMP,
  42. };
  43. #define IIO_MOD_X 0
  44. #define IIO_MOD_LIGHT_BOTH 0
  45. #define IIO_MOD_Y 1
  46. #define IIO_MOD_LIGHT_IR 1
  47. #define IIO_MOD_Z 2
  48. #define IIO_MOD_X_AND_Y 3
  49. #define IIO_MOD_X_ANX_Z 4
  50. #define IIO_MOD_Y_AND_Z 5
  51. #define IIO_MOD_X_AND_Y_AND_Z 6
  52. #define IIO_MOD_X_OR_Y 7
  53. #define IIO_MOD_X_OR_Z 8
  54. #define IIO_MOD_Y_OR_Z 9
  55. #define IIO_MOD_X_OR_Y_OR_Z 10
  56. /* Could add the raw attributes as well - allowing buffer only devices */
  57. enum iio_chan_info_enum {
  58. IIO_CHAN_INFO_SCALE_SHARED,
  59. IIO_CHAN_INFO_SCALE_SEPARATE,
  60. IIO_CHAN_INFO_OFFSET_SHARED,
  61. IIO_CHAN_INFO_OFFSET_SEPARATE,
  62. IIO_CHAN_INFO_CALIBSCALE_SHARED,
  63. IIO_CHAN_INFO_CALIBSCALE_SEPARATE,
  64. IIO_CHAN_INFO_CALIBBIAS_SHARED,
  65. IIO_CHAN_INFO_CALIBBIAS_SEPARATE,
  66. IIO_CHAN_INFO_PEAK_SHARED,
  67. IIO_CHAN_INFO_PEAK_SEPARATE,
  68. IIO_CHAN_INFO_PEAK_SCALE_SHARED,
  69. IIO_CHAN_INFO_PEAK_SCALE_SEPARATE,
  70. };
  71. /**
  72. * struct iio_chan_spec - specification of a single channel
  73. * @type: What type of measurement is the channel making.
  74. * @channel: What number or name do we wish to asign the channel.
  75. * @channel2: If there is a second number for a differential
  76. * channel then this is it. If modified is set then the
  77. * value here specifies the modifier.
  78. * @address: Driver specific identifier.
  79. * @scan_index: Monotonic index to give ordering in scans when read
  80. * from a buffer.
  81. * @scan_type: Sign: 's' or 'u' to specify signed or unsigned
  82. * realbits: Number of valid bits of data
  83. * storage_bits: Realbits + padding
  84. * shift: Shift right by this before masking out
  85. * realbits.
  86. * @info_mask: What information is to be exported about this channel.
  87. * This includes calibbias, scale etc.
  88. * @event_mask: What events can this channel produce.
  89. * @extend_name: Allows labeling of channel attributes with an
  90. * informative name. Note this has no effect codes etc,
  91. * unlike modifiers.
  92. * @processed_val: Flag to specify the data access attribute should be
  93. * *_input rather than *_raw.
  94. * @modified: Does a modifier apply to this channel. What these are
  95. * depends on the channel type. Modifier is set in
  96. * channel2. Examples are IIO_MOD_X for axial sensors about
  97. * the 'x' axis.
  98. * @indexed: Specify the channel has a numerical index. If not,
  99. * the value in channel will be suppressed for attribute
  100. * but not for event codes. Typically set it to 0 when
  101. * the index is false.
  102. */
  103. struct iio_chan_spec {
  104. enum iio_chan_type type;
  105. int channel;
  106. int channel2;
  107. unsigned long address;
  108. int scan_index;
  109. struct {
  110. char sign;
  111. u8 realbits;
  112. u8 storagebits;
  113. u8 shift;
  114. } scan_type;
  115. const long info_mask;
  116. const long event_mask;
  117. const char *extend_name;
  118. unsigned processed_val:1;
  119. unsigned modified:1;
  120. unsigned indexed:1;
  121. };
  122. /* Meant for internal use only */
  123. void __iio_device_attr_deinit(struct device_attribute *dev_attr);
  124. int __iio_device_attr_init(struct device_attribute *dev_attr,
  125. const char *postfix,
  126. struct iio_chan_spec const *chan,
  127. ssize_t (*readfunc)(struct device *dev,
  128. struct device_attribute *attr,
  129. char *buf),
  130. ssize_t (*writefunc)(struct device *dev,
  131. struct device_attribute *attr,
  132. const char *buf,
  133. size_t len),
  134. bool generic);
  135. #define IIO_ST(si, rb, sb, sh) \
  136. { .sign = si, .realbits = rb, .storagebits = sb, .shift = sh }
  137. #define IIO_CHAN(_type, _mod, _indexed, _proc, _name, _chan, _chan2, \
  138. _inf_mask, _address, _si, _stype, _event_mask) \
  139. { .type = _type, \
  140. .modified = _mod, \
  141. .indexed = _indexed, \
  142. .processed_val = _proc, \
  143. .extend_name = _name, \
  144. .channel = _chan, \
  145. .channel2 = _chan2, \
  146. .info_mask = _inf_mask, \
  147. .address = _address, \
  148. .scan_index = _si, \
  149. .scan_type = _stype, \
  150. .event_mask = _event_mask }
  151. #define IIO_CHAN_SOFT_TIMESTAMP(_si) \
  152. { .type = IIO_TIMESTAMP, .channel = -1, \
  153. .scan_index = _si, .scan_type = IIO_ST('s', 64, 64, 0) }
  154. int __iio_add_chan_devattr(const char *postfix,
  155. const char *group,
  156. struct iio_chan_spec const *chan,
  157. ssize_t (*func)(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf),
  160. ssize_t (*writefunc)(struct device *dev,
  161. struct device_attribute *attr,
  162. const char *buf,
  163. size_t len),
  164. int mask,
  165. bool generic,
  166. struct device *dev,
  167. struct list_head *attr_list);
  168. /**
  169. * iio_get_time_ns() - utility function to get a time stamp for events etc
  170. **/
  171. static inline s64 iio_get_time_ns(void)
  172. {
  173. struct timespec ts;
  174. /*
  175. * calls getnstimeofday.
  176. * If hrtimers then up to ns accurate, if not microsecond.
  177. */
  178. ktime_get_real_ts(&ts);
  179. return timespec_to_ns(&ts);
  180. }
  181. /* Device operating modes */
  182. #define INDIO_DIRECT_MODE 0x01
  183. #define INDIO_RING_TRIGGERED 0x02
  184. #define INDIO_RING_HARDWARE_BUFFER 0x08
  185. #define INDIO_ALL_RING_MODES (INDIO_RING_TRIGGERED | INDIO_RING_HARDWARE_BUFFER)
  186. /* Vast majority of this is set by the industrialio subsystem on a
  187. * call to iio_device_register. */
  188. #define IIO_VAL_INT 1
  189. #define IIO_VAL_INT_PLUS_MICRO 2
  190. /**
  191. * struct iio_info - constant information about device
  192. * @driver_module: module structure used to ensure correct
  193. * ownership of chrdevs etc
  194. * @num_interrupt_lines:number of physical interrupt lines from device
  195. * @event_attrs: event control attributes
  196. * @attrs: general purpose device attributes
  197. * @read_raw: function to request a value from the device.
  198. * mask specifies which value. Note 0 means a reading of
  199. * the channel in question. Return value will specify the
  200. * type of value returned by the device. val and val2 will
  201. * contain the elements making up the returned value.
  202. * @write_raw: function to write a value to the device.
  203. * Parameters are the same as for read_raw.
  204. * @read_event_config: find out if the event is enabled.
  205. * @write_event_config: set if the event is enabled.
  206. * @read_event_value: read a value associated with the event. Meaning
  207. * is event dependant. event_code specifies which event.
  208. * @write_event_value: write the value associate with the event.
  209. * Meaning is event dependent.
  210. **/
  211. struct iio_info {
  212. struct module *driver_module;
  213. int num_interrupt_lines;
  214. struct attribute_group *event_attrs;
  215. const struct attribute_group *attrs;
  216. int (*read_raw)(struct iio_dev *indio_dev,
  217. struct iio_chan_spec const *chan,
  218. int *val,
  219. int *val2,
  220. long mask);
  221. int (*write_raw)(struct iio_dev *indio_dev,
  222. struct iio_chan_spec const *chan,
  223. int val,
  224. int val2,
  225. long mask);
  226. int (*read_event_config)(struct iio_dev *indio_dev,
  227. int event_code);
  228. int (*write_event_config)(struct iio_dev *indio_dev,
  229. int event_code,
  230. int state);
  231. int (*read_event_value)(struct iio_dev *indio_dev,
  232. int event_code,
  233. int *val);
  234. int (*write_event_value)(struct iio_dev *indio_dev,
  235. int event_code,
  236. int val);
  237. };
  238. /**
  239. * struct iio_dev - industrial I/O device
  240. * @id: [INTERN] used to identify device internally
  241. * @dev_data: [DRIVER] device specific data
  242. * @modes: [DRIVER] operating modes supported by device
  243. * @currentmode: [DRIVER] current operating mode
  244. * @dev: [DRIVER] device structure, should be assigned a parent
  245. * and owner
  246. * @event_interfaces: [INTERN] event chrdevs associated with interrupt lines
  247. * @ring: [DRIVER] any ring buffer present
  248. * @mlock: [INTERN] lock used to prevent simultaneous device state
  249. * changes
  250. * @available_scan_masks: [DRIVER] optional array of allowed bitmasks
  251. * @trig: [INTERN] current device trigger (ring buffer modes)
  252. * @pollfunc: [DRIVER] function run on trigger being received
  253. * @channels: [DRIVER] channel specification structure table
  254. * @num_channels: [DRIVER] number of chanels specified in @channels.
  255. * @channel_attr_list: [INTERN] keep track of automatically created channel
  256. * attributes.
  257. * @name: [DRIVER] name of the device.
  258. **/
  259. struct iio_dev {
  260. int id;
  261. void *dev_data;
  262. int modes;
  263. int currentmode;
  264. struct device dev;
  265. struct iio_event_interface *event_interfaces;
  266. struct iio_ring_buffer *ring;
  267. struct mutex mlock;
  268. u32 *available_scan_masks;
  269. struct iio_trigger *trig;
  270. struct iio_poll_func *pollfunc;
  271. struct iio_chan_spec const *channels;
  272. int num_channels;
  273. struct list_head channel_attr_list;
  274. const char *name;
  275. const struct iio_info *info;
  276. };
  277. /**
  278. * iio_device_register() - register a device with the IIO subsystem
  279. * @dev_info: Device structure filled by the device driver
  280. **/
  281. int iio_device_register(struct iio_dev *dev_info);
  282. /**
  283. * iio_device_unregister() - unregister a device from the IIO subsystem
  284. * @dev_info: Device structure representing the device.
  285. **/
  286. void iio_device_unregister(struct iio_dev *dev_info);
  287. /**
  288. * iio_push_event() - try to add event to the list for userspace reading
  289. * @dev_info: IIO device structure
  290. * @ev_line: Which event line (hardware interrupt)
  291. * @ev_code: What event
  292. * @timestamp: When the event occurred
  293. **/
  294. int iio_push_event(struct iio_dev *dev_info,
  295. int ev_line,
  296. int ev_code,
  297. s64 timestamp);
  298. /* Used to distinguish between bipolar and unipolar scan elemenents.
  299. * Whilst this may seem obvious, we may well want to change the representation
  300. * in the future!*/
  301. #define IIO_SIGNED(a) -(a)
  302. #define IIO_UNSIGNED(a) (a)
  303. extern dev_t iio_devt;
  304. extern struct bus_type iio_bus_type;
  305. /**
  306. * iio_put_device() - reference counted deallocation of struct device
  307. * @dev: the iio_device containing the device
  308. **/
  309. static inline void iio_put_device(struct iio_dev *dev)
  310. {
  311. if (dev)
  312. put_device(&dev->dev);
  313. };
  314. /**
  315. * to_iio_dev() - get iio_dev for which we have the struct device
  316. * @d: the struct device
  317. **/
  318. static inline struct iio_dev *to_iio_dev(struct device *d)
  319. {
  320. return container_of(d, struct iio_dev, dev);
  321. };
  322. /**
  323. * iio_dev_get_devdata() - helper function gets device specific data
  324. * @d: the iio_dev associated with the device
  325. **/
  326. static inline void *iio_dev_get_devdata(struct iio_dev *d)
  327. {
  328. return d->dev_data;
  329. }
  330. /* Can we make this smaller? */
  331. #define IIO_ALIGN L1_CACHE_BYTES
  332. /**
  333. * iio_allocate_device() - allocate an iio_dev from a driver
  334. * @sizeof_priv: Space to allocate for private structure.
  335. **/
  336. struct iio_dev *iio_allocate_device(int sizeof_priv);
  337. static inline void *iio_priv(const struct iio_dev *dev)
  338. {
  339. return (char *)dev + ALIGN(sizeof(struct iio_dev), IIO_ALIGN);
  340. }
  341. static inline struct iio_dev *iio_priv_to_dev(void *priv)
  342. {
  343. return (struct iio_dev *)((char *)priv -
  344. ALIGN(sizeof(struct iio_dev), IIO_ALIGN));
  345. }
  346. /**
  347. * iio_free_device() - free an iio_dev from a driver
  348. * @dev: the iio_dev associated with the device
  349. **/
  350. void iio_free_device(struct iio_dev *dev);
  351. /**
  352. * iio_put() - internal module reference count reduce
  353. **/
  354. void iio_put(void);
  355. /**
  356. * iio_get() - internal module reference count increase
  357. **/
  358. void iio_get(void);
  359. /**
  360. * iio_device_get_chrdev_minor() - get an unused minor number
  361. **/
  362. int iio_device_get_chrdev_minor(void);
  363. void iio_device_free_chrdev_minor(int val);
  364. /**
  365. * iio_ring_enabled() - helper function to test if any form of ring is enabled
  366. * @dev_info: IIO device info structure for device
  367. **/
  368. static inline bool iio_ring_enabled(struct iio_dev *dev_info)
  369. {
  370. return dev_info->currentmode
  371. & (INDIO_RING_TRIGGERED
  372. | INDIO_RING_HARDWARE_BUFFER);
  373. };
  374. struct ida;
  375. int iio_get_new_ida_val(struct ida *this_ida);
  376. void iio_free_ida_val(struct ida *this_ida, int id);
  377. #endif /* _INDUSTRIAL_IO_H_ */