-
Notifications
You must be signed in to change notification settings - Fork 0
/
hhg_lcd.c
871 lines (736 loc) · 20.8 KB
/
hhg_lcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
243
244
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/***************************************************************************
*
* Hi Happy Garden LCD HITACHI HD44780U
* Copyright (C) 2023 Antonio Salsi <[email protected]>
*
* 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 3 of the License
*
* 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, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
#include "hhg_lcd.h"
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/cdev.h>
#ifdef pr_fmt
#undef pr_fmt
#define pr_fmt(fmt) "hhg_lcd: " fmt
#endif
#define HHG_IS_VALID(gpio, msg) \
if (!gpio_is_valid(gpio)) \
{ \
pr_err("GPIO " msg " wrong pin:%d", gpio); \
return -EINVAL; \
}
//LCD MANAGEMENT
static bool data_mode_8_bit = false;
static short gpio_rs = -1;
static short gpio_en = -1;
static short gpio_db0 = -1;
static short gpio_db1 = -1;
static short gpio_db2 = -1;
static short gpio_db3 = -1;
static short gpio_db4 = -1;
static short gpio_db5 = -1;
static short gpio_db6 = -1;
static short gpio_db7 = -1;
static char msg_to_display[(HHG_ROWS * HHG_COLS) + 2] = { [ 0 ... (HHG_ROWS * HHG_COLS) + 1] = 0};
// static decl
/**
* @brief Initializes the LCD in 8-bit mode.
*
* This function initializes the LCD in 8-bit mode.
*
* @return `true` if the initialization was successful, `false` otherwise.
*/
static bool hhg_lcd_init(void);
/**
* @brief Initializes the LCD in 8-bit mode.
*
* This function initializes the LCD in 8-bit mode.
*
* @return `true` if the initialization was successful, `false` otherwise.
*/
static bool hhg_lcd_init_8_bit(void);
/**
* @brief Initializes the LCD in 4-bit mode.
*
* This function initializes the LCD in 4-bit mode.
*
* @return `true` if the initialization was successful, `false` otherwise.
*/
static bool hhg_lcd_init_4_bit(void);
/**
* @brief Frees the pin used by the HHG LCD.
*
* This function frees the specified GPIO pin used by the HHG LCD.
*
* @param gpio_number The GPIO pin number to be freed.
*/
static void hhg_lcd_pin_free(u8 gpio_number);
/**
* @brief Initializes the LCD in 4-bit mode.
*
*/
static void hhg_lcd_free(void);
/**
* @brief Sets up the pin for the HHG LCD.
*
* This function sets up the specified GPIO pin for the HHG LCD.
*
* @param gpio_number The GPIO pin number to be set up.
* @param gpio_direction The direction of the GPIO pin (input or output).
* @return `true` if the operation was successful, `false` otherwise.
*/
static bool hhg_lcd_pin_setup(u8 gpio_number, u8 gpio_direction);
/**
* @brief Sends one nibble of data to the LCD.
*
* This function sends one nibble of data to the LCD.
*
* @param byte The nibble of data to be sent.
*/
static void hhg_lcd_send_nibble(u8 byte, bool rs_value);
/**
* @brief Sends one byte of data to the LCD.
*
* This function sends one byte of data to the LCD.
*
* @param byte The byte of data to be sent.
*/
static void hhg_lcd_send_byte(u8 byte, bool rs_value);
bool hhg_lcd_init(void)
{
if(
gpio_db0 > -1
&& gpio_db1 > -1
&& gpio_db2 > -1
&& gpio_db3 > -1
&& gpio_db4 > -1
&& gpio_db5 > -1
&& gpio_db6 > -1
&& gpio_db7 > -1
)
{
data_mode_8_bit = true;
}
else if(
gpio_db0 == -1
&& gpio_db1 == -1
&& gpio_db2 == -1
&& gpio_db3 == -1
&& gpio_db4 > -1
&& gpio_db5 > -1
&& gpio_db6 > -1
&& gpio_db7 > -1
)
{
data_mode_8_bit = false;
}
else
{
pr_err("number of configured pins not suitable for 4-bit or 8-bit configuration");
return false;
}
if(gpio_rs == -1)
{
pr_err("GPIO RS mandatory");
return false;
}
if(gpio_en == -1)
{
pr_err("GPIO RS mandatory");
return false;
}
if(data_mode_8_bit)
{
//init 8 bit
if(!hhg_lcd_init_8_bit())
{
pr_err("Init 4 bit data error");
return false;
}
}
else
{
//init 4 bit
if(!hhg_lcd_init_4_bit())
{
pr_err("Init 4 bit data error");
return false;
}
}
return true;
}
bool hhg_lcd_init_8_bit(void)
{
pr_info("init 8 bit data mode done"
" gpio_rs:%d"
" gpio_en:%d"
" gpio_db0:%d"
" gpio_db1:%d"
" gpio_db2:%d"
" gpio_db3:%d"
" gpio_db4:%d"
" gpio_db5:%d"
" gpio_db6:%d"
" gpio_db7:%d"
, gpio_rs
, gpio_en
, gpio_db0
, gpio_db1
, gpio_db2
, gpio_db3
, gpio_db4
, gpio_db5
, gpio_db6
, gpio_db7
);
if(!hhg_lcd_pin_setup(gpio_rs, 0))
{
pr_err("Error to set gpio_rs");
return false;
}
if(!hhg_lcd_pin_setup(gpio_en, 0))
{
pr_err("Error to set gpio_en");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db0, 0))
{
pr_err("Error to set gpio_db0");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db1, 0))
{
pr_err("Error to set gpio_db1");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db2, 0))
{
pr_err("Error to set gpio_db2");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db4, 0))
{
pr_err("Error to set gpio_db4");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db4, 0))
{
pr_err("Error to set gpio_db4");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db5, 0))
{
pr_err("Error to set gpio_db5");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db6, 0))
{
pr_err("Error to set gpio_db6");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db7, 0))
{
pr_err("Error to set gpio_db7");
return false;
}
//for main timing see manual page 45
//for timing and command see table 6 page 24
usleep_range(45*1000, 55*1000); // Wait for more than 40 ms
hhg_lcd_send_command(0x30); // Function set
usleep_range(5*2000, 6*1000); // Wait for more than 4,1 ms
hhg_lcd_send_command(0x30); // Function set
usleep_range(150, 200); //Wait for more than 100 μs
hhg_lcd_send_command(0x30); // Function set
usleep_range(150, 200); //Wait for more than 100 μs
hhg_lcd_send_command(0x38); // Function set (Interface is 8 bits long. Specify the number of display lines and character font.)
usleep_range(150, 200); //Wait for more than 100 μs
hhg_lcd_send_command(0x08); // Display off
usleep_range(150, 200); //Wait for more than 100 μs
hhg_lcd_clear();
usleep_range(150, 200);
hhg_lcd_send_command(0x06); // Entry mode set
hhg_lcd_set_flags(HHG_LCD_DISPLAY_ON);
return true;
}
bool hhg_lcd_init_4_bit(void)
{
pr_info("init 4 bit data mode done"
" gpio_rs:%d"
" gpio_en:%d"
" gpio_db4:%d"
" gpio_db5:%d"
" gpio_db6:%d"
" gpio_db7:%d"
, gpio_rs
, gpio_en
, gpio_db4
, gpio_db5
, gpio_db6
, gpio_db7
);
if(!hhg_lcd_pin_setup(gpio_rs, 0))
{
pr_err("Error to set gpio_rs");
return false;
}
if(!hhg_lcd_pin_setup(gpio_en, 0))
{
pr_err("Error to set gpio_en");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db4, 0))
{
pr_err("Error to set gpio_db4");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db5, 0))
{
pr_err("Error to set gpio_db5");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db6, 0))
{
pr_err("Error to set gpio_db6");
return false;
}
if(!hhg_lcd_pin_setup(gpio_db7, 0))
{
pr_err("Error to set gpio_db7");
return false;
}
//for main timing see manual page 45
//for timing and command see table 6 page 24
usleep_range(45*1000, 55*1000); // Wait for more than 40 ms
hhg_lcd_send_command(0x20); // Function set
usleep_range(5*1000, 6*1000); //Wait for more than 4.1 ms
hhg_lcd_send_command(0x20); // Function set
usleep_range(150, 200);
hhg_lcd_send_command(0x20); // Instruction 0011b (Function set)
usleep_range(150, 200); //Wait for more than 100 μs
hhg_lcd_send_command(0x20);
hhg_lcd_send_command(0x80);
usleep_range(41*1000, 50*1000);
hhg_lcd_set_flags(HHG_LCD_DISPLAY_OFF);
usleep_range(150, 200);
hhg_lcd_clear();
usleep_range(150, 200);
/* Entry mode set */
hhg_lcd_send_command(0x00); // Instruction 0000b
hhg_lcd_send_command(0x60); /* Instruction 01(I/D)Sb -> 0110b
Set I/D = 1, or increment or decrement DDRAM address by 1
Set S = 0, or no display shift
*/
usleep_range(150, 200);
hhg_lcd_set_flags(HHG_LCD_DISPLAY_ON);
return true;
}
inline static void hhg_lcd_pin_free(u8 gpio_number)
{
gpio_unexport(gpio_number);
gpio_free(gpio_number);
}
static void hhg_lcd_free(void)
{
hhg_lcd_pin_free(gpio_rs);
hhg_lcd_pin_free(gpio_en);
if(data_mode_8_bit)
{
hhg_lcd_pin_free(gpio_db0);
hhg_lcd_pin_free(gpio_db1);
hhg_lcd_pin_free(gpio_db2);
hhg_lcd_pin_free(gpio_db3);
}
hhg_lcd_pin_free(gpio_db4);
hhg_lcd_pin_free(gpio_db5);
hhg_lcd_pin_free(gpio_db6);
hhg_lcd_pin_free(gpio_db7);
}
bool hhg_lcd_pin_setup(u8 gpio_number, u8 gpio_direction)
{
u8 ret;
ret = gpio_request( gpio_number, "GPIO request");
if( ret != 0 ) {
pr_err("failed to request GPIO %d \n", gpio_number );
return false;
}
ret = gpio_export( gpio_number, 0);
if( ret != 0 ) {
pr_err("failed to export GPIO %d \n", gpio_number );
return false;
}
ret = gpio_direction_output( gpio_number, gpio_direction);
if( ret != 0 ) {
pr_err("failed to set GPIO direction %d \n", gpio_number );
return false;
}
gpio_set_value(gpio_number, 0);
return true;
}
void hhg_lcd_send_byte(u8 byte, bool rs_value)
{
gpio_set_value(gpio_db0, (byte >> 0 ) & 0x01);
gpio_set_value(gpio_db1, (byte >> 1 ) & 0x01);
gpio_set_value(gpio_db2, (byte >> 2 ) & 0x01);
gpio_set_value(gpio_db3, (byte >> 3 ) & 0x01);
gpio_set_value(gpio_db4, (byte >> 4 ) & 0x01);
gpio_set_value(gpio_db5, (byte >> 5 ) & 0x01);
gpio_set_value(gpio_db6, (byte >> 6 ) & 0x01);
gpio_set_value(gpio_db7, (byte >> 7 ) & 0x01);
gpio_set_value(gpio_rs, rs_value);
usleep_range(5, 10);
gpio_set_value(gpio_en, 1);
usleep_range(5, 10);
gpio_set_value(gpio_en, 0);
}
void hhg_lcd_send_nibble(u8 byte, bool rs_value)
{
gpio_set_value(gpio_db4, (byte >> 0 ) & 0x01);
gpio_set_value(gpio_db5, (byte >> 1 ) & 0x01);
gpio_set_value(gpio_db6, (byte >> 2 ) & 0x01);
gpio_set_value(gpio_db7, (byte >> 3 ) & 0x01);
gpio_set_value(gpio_rs, rs_value);
usleep_range(5, 10);
gpio_set_value(gpio_en, 1);
usleep_range(5, 10);
gpio_set_value(gpio_en, 0);
}
inline void hhg_lcd_send_command(u8 command)
{
if(data_mode_8_bit)
{
usleep_range(1000, 1500);
hhg_lcd_send_byte(command, HHG_COMMAND_MODE);
}
else
{
usleep_range(1000, 1500);
hhg_lcd_send_nibble(command >> 4, HHG_COMMAND_MODE);
}
}
void hhg_lcd_send_char(char byte)
{
if(data_mode_8_bit)
{
hhg_lcd_send_byte(byte, HHG_DATA_MODE);
}
else
{
hhg_lcd_send_nibble(byte >> 4, HHG_DATA_MODE); // upper
usleep_range(1000, 1500);
hhg_lcd_send_nibble(byte & 0x0F, HHG_DATA_MODE); // lower
}
}
EXPORT_SYMBOL(hhg_lcd_send_char);
void hhg_lcd_send_str(const char buff[])
{
if(!buff)
{
return;
}
hhg_lcd_clear();
enum hhg_row row = HHG_FIRST_ROW;
hhg_lcd_select_row(row);
u8 col_counter = 0;
const char* cursor = buff;
while(*cursor != '\0' && row <= HHG_ROWS)
{
if(col_counter >= HHG_COLS || *cursor == '\n')
{
row++;
col_counter = 0;
pr_info("row:%u", row);
hhg_lcd_select_row(row);
if(*cursor == '\n')
{
cursor++;
continue;
}
}
pr_info("col:%u - %c", col_counter, *cursor);
hhg_lcd_send_char(*cursor);
col_counter++;
cursor++;
}
}
EXPORT_SYMBOL(hhg_lcd_send_str);
void hhg_lcd_clear(void)
{
if(data_mode_8_bit)
{
hhg_lcd_send_command(0x01);
}
else
{
hhg_lcd_send_command(0x00);
hhg_lcd_send_command(0x10);
}
}
EXPORT_SYMBOL(hhg_lcd_clear);
void hhg_lcd_select_row(enum hhg_row row)
{
if(data_mode_8_bit)
{
switch (row)
{
case HHG_FIRST_ROW:
hhg_lcd_send_command(0x30);
break;
case HHG_SECOND_ROW:
hhg_lcd_send_command(0x38);
break;
default:
break;
}
}
else
{
switch (row)
{
case HHG_FIRST_ROW:
hhg_lcd_send_command(0x80);
hhg_lcd_send_command(0x00);
break;
case HHG_SECOND_ROW:
hhg_lcd_send_command(0xC0);
hhg_lcd_send_command(0x00);
break;
default:
break;
}
}
}
EXPORT_SYMBOL(hhg_lcd_select_row);
void hhg_lcd_set_flags(u8 flags)
{
if(data_mode_8_bit)
{
hhg_lcd_send_command(0x08 | (flags & 0x07));
}
else
{
hhg_lcd_send_command(0x00); //upper
hhg_lcd_send_command(0x80 | ((flags & 0x07) << 4)); //lower
}
usleep_range(45, 55);
}
EXPORT_SYMBOL(hhg_lcd_set_flags);
//MODULE
// data
static dev_t hhg_dev = 0; ///< Device number for the HHG device.
static struct class* hhg_class; ///< Pointer to the device class for the HHG device.
static struct cdev hhg_cdev; ///< Character device structure for the HHG device.
static atomic_t device_busy = ATOMIC_INIT(0); ///< Atomic variable to track device availability.
/**
* @brief Uevent callback function for HHG LCD device.
*
* This function is called when a uevent (user event) occurs for the HHG LCD device.
* It handles changing the access permissions in user space.
*
* @param dev Pointer to the device structure.
* @param env Pointer to the kobj_uevent_env structure containing the uevent environment.
* @return The return value indicates the success or failure of the uevent handling.
*/
static int hhg_lcd_uevent(struct device* dev, struct kobj_uevent_env* env);
/**
* @brief File operations open function for HHG LCD device.
*
* This function is called when the HHG LCD device file is opened.
*
* @param inode Pointer to the inode structure.
* @param file Pointer to the file structure.
* @return The return value indicates the success or failure of the file open operation.
*/
static int hhg_lcd_fops_open(struct inode* inode, struct file* file);
/**
* @brief File operations release function for HHG LCD device.
*
* This function is called when the HHG LCD device file is closed.
*
* @param inode Pointer to the inode structure.
* @param file Pointer to the file structure.
* @return The return value indicates the success or failure of the file release operation.
*/
static int hhg_lcd_fops_release(struct inode* inode, struct file* file);
/**
* @brief File operations read function for HHG LCD device.
*
* This function is called when a read operation is performed on the HHG LCD device file.
*
* @param filp Pointer to the file structure.
* @param buff User buffer to store the read data.
* @param len Number of bytes to read.
* @param off Pointer to the file offset.
* @return The number of bytes read, or an error code indicating the failure of the read operation.
*/
static ssize_t hhg_lcd_fops_read(struct file* filp, char __user* buff, size_t len, loff_t* off);
/**
* @brief File operations write function for HHG LCD device.
*
* This function is called when a write operation is performed on the HHG LCD device file.
*
* @param filp Pointer to the file structure.
* @param buff User buffer containing the data to be written.
* @param len Number of bytes to write.
* @param off Pointer to the file offset.
* @return The number of bytes written, or an error code indicating the failure of the write operation.
*/
static ssize_t hhg_lcd_fops_write(struct file* filp, const char* buff, size_t len, loff_t* off);
module_param(gpio_rs, short, 0660);
MODULE_PARM_DESC(gpio_rs, "GPIO RS - Select registers");
module_param(gpio_en, short, 0660);
MODULE_PARM_DESC(gpio_en, "GPIO EN - Start data read/write");
module_param(gpio_db0, short, 0660);
MODULE_PARM_DESC(gpio_db0, "GPIO DB0 - bit 0");
module_param(gpio_db1, short, 0660);
MODULE_PARM_DESC(gpio_db1, "GPIO DB1 - bit 1");
module_param(gpio_db2, short, 0660);
MODULE_PARM_DESC(gpio_db2, "GPIO DB2 - bit 2");
module_param(gpio_db3, short, 0660);
MODULE_PARM_DESC(gpio_db3, "GPIO DB3 - bit 3");
module_param(gpio_db4, short, 0660);
MODULE_PARM_DESC(gpio_db4, "GPIO DB4 - bit 4");
module_param(gpio_db5, short, 0660);
MODULE_PARM_DESC(gpio_db5, "GPIO DB5 - bit 5");
module_param(gpio_db6, short, 0660);
MODULE_PARM_DESC(gpio_db6, "GPIO DB6 - bit 6");
module_param(gpio_db7, short, 0660);
MODULE_PARM_DESC(gpio_db7, "GPIO DB7 - bit 7");
// File operation structure
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = hhg_lcd_fops_read,
.write = hhg_lcd_fops_write,
.open = hhg_lcd_fops_open,
.release = hhg_lcd_fops_release
};
/*
* Module Init function
*/
static int __init hhg_lcd_module_init(void)
{
/*Allocating Major number*/
if ((alloc_chrdev_region(&hhg_dev, HHG_MAJOR_NUM_START, HHG_MINOR_NUM_COUNT, HHG_DRIVER_NAME)) < 0)
{
pr_err("cannot allocate major number\n");
goto r_unreg;
}
pr_info("Major = %d Minor = %d \n", MAJOR(hhg_dev), MINOR(hhg_dev));
/*Creating cdev structure*/
cdev_init(&hhg_cdev, &fops);
/*Adding character device to the system*/
if ((cdev_add(&hhg_cdev, hhg_dev, 1)) < 0)
{
pr_err("cannot add the device to the system\n");
goto r_dev;
}
/*Creating struct class*/
if ((hhg_class = class_create(THIS_MODULE, HHG_CLASS_NAME)) == NULL)
{
pr_err("cannot create the struct class\n");
goto r_class;
}
hhg_class->dev_uevent = hhg_lcd_uevent;
/*Creating device*/
if ((device_create(hhg_class, NULL, hhg_dev, NULL, HHG_DRIVER_NAME)) == NULL)
{
pr_err("cannot create the Device \n");
goto r_device;
}
if(!hhg_lcd_init())
{
pr_err("cannot init LCD\n");
goto r_device;
}
return 0;
r_device:
device_destroy(hhg_class, hhg_dev);
r_class:
class_destroy(hhg_class);
r_dev:
cdev_del(&hhg_cdev);
r_unreg:
unregister_chrdev_region(hhg_dev, 1);
return -ENXIO;
}
module_init(hhg_lcd_module_init);
/*
* Module exit function
*/
static void __exit hhg_lcd_module_exit(void)
{
hhg_lcd_clear();
hhg_lcd_set_flags(HHG_LCD_DISPLAY_OFF);
hhg_lcd_free();
device_destroy(hhg_class, hhg_dev);
class_destroy(hhg_class);
cdev_del(&hhg_cdev);
unregister_chrdev_region(hhg_dev, 1);
pr_info("exit");
}
module_exit(hhg_lcd_module_exit);
int hhg_lcd_fops_open(struct inode *inode, struct file *file)
{
if (atomic_read(&device_busy) > 0)
{
pr_err("device busy");
return -EBUSY;
}
atomic_inc(&device_busy);
pr_info("open:%u\n", atomic_read(&device_busy));
return 0;
}
int hhg_lcd_fops_release(struct inode *inode, struct file *file)
{
atomic_sub(1, &device_busy);
pr_info("release:%u\n", atomic_read(&device_busy));
return 0;
}
ssize_t hhg_lcd_fops_read(struct file *filp, char __user *buff, size_t len, loff_t *off)
{
return simple_read_from_buffer(buff, len, off, msg_to_display, strlen(msg_to_display));
}
ssize_t hhg_lcd_fops_write(struct file *filp, const char *buff, size_t len, loff_t *off)
{
size_t bytes_to_write = 0;
memset(msg_to_display, '\0', sizeof(msg_to_display));
if(len > sizeof(msg_to_display) - 1)
{
bytes_to_write = sizeof(msg_to_display) - 1;
}
else
{
bytes_to_write = len;
}
ssize_t not_written = len - simple_write_to_buffer(msg_to_display, sizeof(msg_to_display) - 1, off, buff, len);
if(not_written > 0)
{
pr_warn("not written all data exceed for: %ld char", not_written);
}
hhg_lcd_send_str(msg_to_display);
return len;
}
int hhg_lcd_uevent(struct device *dev, struct kobj_uevent_env *env)
{
add_uevent_var(env, "DEVMODE=%#o", 0666);
return 0;
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Antonio Salsi <[email protected]>");
MODULE_DESCRIPTION("Driver for LCD 16x2 management with chip HITACHI HD44780U compatible.");
MODULE_INFO(intree, "Y");
MODULE_VERSION("0.90.1");