2222#include <linux/cdev.h>
2323#include <linux/slab.h>
2424#include <linux/anon_inodes.h>
25+ #include <linux/debugfs.h>
2526#include "iio.h"
2627#include "iio_core.h"
2728#include "iio_core_trigger.h"
@@ -39,6 +40,8 @@ struct bus_type iio_bus_type = {
3940};
4041EXPORT_SYMBOL (iio_bus_type );
4142
43+ static struct dentry * iio_debugfs_dentry ;
44+
4245static const char * const iio_data_type_name [] = {
4346 [IIO_RAW ] = "raw" ,
4447 [IIO_PROCESSED ] = "input" ,
@@ -129,6 +132,8 @@ static int __init iio_init(void)
129132 goto error_unregister_bus_type ;
130133 }
131134
135+ iio_debugfs_dentry = debugfs_create_dir ("iio" , NULL );
136+
132137 return 0 ;
133138
134139error_unregister_bus_type :
@@ -142,6 +147,84 @@ static void __exit iio_exit(void)
142147 if (iio_devt )
143148 unregister_chrdev_region (iio_devt , IIO_DEV_MAX );
144149 bus_unregister (& iio_bus_type );
150+ debugfs_remove (iio_debugfs_dentry );
151+ }
152+
153+ #if defined(CONFIG_DEBUG_FS )
154+ static int iio_debugfs_open (struct inode * inode , struct file * file )
155+ {
156+ if (inode -> i_private )
157+ file -> private_data = inode -> i_private ;
158+
159+ return 0 ;
160+ }
161+
162+ static ssize_t iio_debugfs_read_reg (struct file * file , char __user * userbuf ,
163+ size_t count , loff_t * ppos )
164+ {
165+ struct iio_dev * indio_dev = file -> private_data ;
166+ char buf [20 ];
167+ unsigned val = 0 ;
168+ ssize_t len ;
169+ int ret ;
170+
171+ ret = indio_dev -> info -> debugfs_reg_access (indio_dev ,
172+ indio_dev -> cached_reg_addr ,
173+ 0 , & val );
174+ if (ret )
175+ dev_err (indio_dev -> dev .parent , "%s: read failed\n" , __func__ );
176+
177+ len = snprintf (buf , sizeof (buf ), "0x%X\n" , val );
178+
179+ return simple_read_from_buffer (userbuf , count , ppos , buf , len );
180+ }
181+
182+ static ssize_t iio_debugfs_write_reg (struct file * file ,
183+ const char __user * userbuf , size_t count , loff_t * ppos )
184+ {
185+ struct iio_dev * indio_dev = file -> private_data ;
186+ unsigned reg , val ;
187+ char buf [80 ];
188+ int ret ;
189+
190+ count = min_t (size_t , count , (sizeof (buf )- 1 ));
191+ if (copy_from_user (buf , userbuf , count ))
192+ return - EFAULT ;
193+
194+ buf [count ] = 0 ;
195+
196+ ret = sscanf (buf , "%i %i" , & reg , & val );
197+
198+ switch (ret ) {
199+ case 1 :
200+ indio_dev -> cached_reg_addr = reg ;
201+ break ;
202+ case 2 :
203+ indio_dev -> cached_reg_addr = reg ;
204+ ret = indio_dev -> info -> debugfs_reg_access (indio_dev , reg ,
205+ val , NULL );
206+ if (ret ) {
207+ dev_err (indio_dev -> dev .parent , "%s: write failed\n" ,
208+ __func__ );
209+ return ret ;
210+ }
211+ break ;
212+ default :
213+ return - EINVAL ;
214+ }
215+
216+ return count ;
217+ }
218+
219+ static const struct file_operations iio_debugfs_reg_fops = {
220+ .open = iio_debugfs_open ,
221+ .read = iio_debugfs_read_reg ,
222+ .write = iio_debugfs_write_reg ,
223+ };
224+
225+ static void iio_device_unregister_debugfs (struct iio_dev * indio_dev )
226+ {
227+ debugfs_remove_recursive (indio_dev -> debugfs_dentry );
145228}
146229
147230static ssize_t iio_read_channel_ext_info (struct device * dev ,
@@ -171,6 +254,49 @@ static ssize_t iio_write_channel_ext_info(struct device *dev,
171254 return ext_info -> write (indio_dev , this_attr -> c , buf , len );
172255}
173256
257+ static int iio_device_register_debugfs (struct iio_dev * indio_dev )
258+ {
259+ struct dentry * d ;
260+
261+ if (indio_dev -> info -> debugfs_reg_access == NULL )
262+ return 0 ;
263+
264+ if (IS_ERR (iio_debugfs_dentry ))
265+ return 0 ;
266+
267+ indio_dev -> debugfs_dentry =
268+ debugfs_create_dir (dev_name (& indio_dev -> dev ),
269+ iio_debugfs_dentry );
270+ if (IS_ERR (indio_dev -> debugfs_dentry ))
271+ return PTR_ERR (indio_dev -> debugfs_dentry );
272+
273+ if (indio_dev -> debugfs_dentry == NULL ) {
274+ dev_warn (indio_dev -> dev .parent ,
275+ "Failed to create debugfs directory\n" );
276+ return - EFAULT ;
277+ }
278+
279+ d = debugfs_create_file ("direct_reg_access" , 0644 ,
280+ indio_dev -> debugfs_dentry ,
281+ indio_dev , & iio_debugfs_reg_fops );
282+ if (!d ) {
283+ iio_device_unregister_debugfs (indio_dev );
284+ return - ENOMEM ;
285+ }
286+
287+ return 0 ;
288+ }
289+ #else
290+ static int iio_device_register_debugfs (struct iio_dev * indio_dev )
291+ {
292+ return 0 ;
293+ }
294+
295+ static void iio_device_unregister_debugfs (struct iio_dev * indio_dev )
296+ {
297+ }
298+ #endif /* CONFIG_DEBUG_FS */
299+
174300static ssize_t iio_read_channel_info (struct device * dev ,
175301 struct device_attribute * attr ,
176302 char * buf )
@@ -618,6 +744,7 @@ static void iio_dev_release(struct device *device)
618744 iio_device_unregister_trigger_consumer (indio_dev );
619745 iio_device_unregister_eventset (indio_dev );
620746 iio_device_unregister_sysfs (indio_dev );
747+ iio_device_unregister_debugfs (indio_dev );
621748}
622749
623750static struct device_type iio_dev_type = {
@@ -734,11 +861,17 @@ int iio_device_register(struct iio_dev *indio_dev)
734861 /* configure elements for the chrdev */
735862 indio_dev -> dev .devt = MKDEV (MAJOR (iio_devt ), indio_dev -> id );
736863
864+ ret = iio_device_register_debugfs (indio_dev );
865+ if (ret ) {
866+ dev_err (indio_dev -> dev .parent ,
867+ "Failed to register debugfs interfaces\n" );
868+ goto error_ret ;
869+ }
737870 ret = iio_device_register_sysfs (indio_dev );
738871 if (ret ) {
739872 dev_err (indio_dev -> dev .parent ,
740873 "Failed to register sysfs interfaces\n" );
741- goto error_ret ;
874+ goto error_unreg_debugfs ;
742875 }
743876 ret = iio_device_register_eventset (indio_dev );
744877 if (ret ) {
@@ -765,6 +898,8 @@ int iio_device_register(struct iio_dev *indio_dev)
765898 iio_device_unregister_eventset (indio_dev );
766899error_free_sysfs :
767900 iio_device_unregister_sysfs (indio_dev );
901+ error_unreg_debugfs :
902+ iio_device_unregister_debugfs (indio_dev );
768903error_ret :
769904 return ret ;
770905}
0 commit comments