w1_therm.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * w1_therm.c
  4. *
  5. * Copyright (c) 2004 Evgeniy Polyakov <[email protected]>
  6. */
  7. #include <asm/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/sched.h>
  12. #include <linux/device.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/string.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/w1.h>
  20. #define W1_THERM_DS18S20 0x10
  21. #define W1_THERM_DS1822 0x22
  22. #define W1_THERM_DS18B20 0x28
  23. #define W1_THERM_DS1825 0x3B
  24. #define W1_THERM_DS28EA00 0x42
  25. /*
  26. * Allow the strong pullup to be disabled, but default to enabled.
  27. * If it was disabled a parasite powered device might not get the require
  28. * current to do a temperature conversion. If it is enabled parasite powered
  29. * devices have a better chance of getting the current required.
  30. * In case the parasite power-detection is not working (seems to be the case
  31. * for some DS18S20) the strong pullup can also be forced, regardless of the
  32. * power state of the devices.
  33. *
  34. * Summary of options:
  35. * - strong_pullup = 0 Disable strong pullup completely
  36. * - strong_pullup = 1 Enable automatic strong pullup detection
  37. * - strong_pullup = 2 Force strong pullup
  38. */
  39. static int w1_strong_pullup = 1;
  40. module_param_named(strong_pullup, w1_strong_pullup, int, 0);
  41. /* Counter for devices supporting bulk reading */
  42. static u16 bulk_read_device_counter; /* =0 as per C standard */
  43. /* This command should be in public header w1.h but is not */
  44. #define W1_RECALL_EEPROM 0xB8
  45. /* Nb of try for an operation */
  46. #define W1_THERM_MAX_TRY 5
  47. /* ms delay to retry bus mutex */
  48. #define W1_THERM_RETRY_DELAY 20
  49. /* delay in ms to write in EEPROM */
  50. #define W1_THERM_EEPROM_WRITE_DELAY 10
  51. #define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */
  52. #define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */
  53. #define BULK_TRIGGER_CMD "trigger" /* cmd to trigger a bulk read */
  54. #define MIN_TEMP -55 /* min temperature that can be measured */
  55. #define MAX_TEMP 125 /* max temperature that can be measured */
  56. /* Allowed values for sysfs conv_time attribute */
  57. #define CONV_TIME_DEFAULT 0
  58. #define CONV_TIME_MEASURE 1
  59. /* Bits in sysfs "features" value */
  60. #define W1_THERM_CHECK_RESULT 1 /* Enable conversion success check */
  61. #define W1_THERM_POLL_COMPLETION 2 /* Poll for conversion completion */
  62. #define W1_THERM_FEATURES_MASK 3 /* All values mask */
  63. /* Poll period in milliseconds. Should be less then a shortest operation on the device */
  64. #define W1_POLL_PERIOD 32
  65. #define W1_POLL_CONVERT_TEMP 2000 /* Timeout for W1_CONVERT_TEMP, ms */
  66. #define W1_POLL_RECALL_EEPROM 500 /* Timeout for W1_RECALL_EEPROM, ms*/
  67. /* Masks for resolution functions, work with all devices */
  68. /* Bit mask for config register for all devices, bits 7,6,5 */
  69. #define W1_THERM_RESOLUTION_MASK 0xE0
  70. /* Bit offset of resolution in config register for all devices */
  71. #define W1_THERM_RESOLUTION_SHIFT 5
  72. /* Bit offset of resolution in config register for all devices */
  73. #define W1_THERM_RESOLUTION_SHIFT 5
  74. /* Add this to bit value to get resolution */
  75. #define W1_THERM_RESOLUTION_MIN 9
  76. /* Maximum allowed value */
  77. #define W1_THERM_RESOLUTION_MAX 14
  78. /* Helpers Macros */
  79. /*
  80. * return a pointer on the slave w1_therm_family_converter struct:
  81. * always test family data existence before using this macro
  82. */
  83. #define SLAVE_SPECIFIC_FUNC(sl) \
  84. (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
  85. /*
  86. * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown
  87. * always test family data existence before using this macro
  88. */
  89. #define SLAVE_POWERMODE(sl) \
  90. (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
  91. /*
  92. * return the resolution in bit of the sl slave : <0 unknown
  93. * always test family data existence before using this macro
  94. */
  95. #define SLAVE_RESOLUTION(sl) \
  96. (((struct w1_therm_family_data *)(sl->family_data))->resolution)
  97. /*
  98. * return the conv_time_override of the sl slave
  99. * always test family data existence before using this macro
  100. */
  101. #define SLAVE_CONV_TIME_OVERRIDE(sl) \
  102. (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override)
  103. /*
  104. * return the features of the sl slave
  105. * always test family data existence before using this macro
  106. */
  107. #define SLAVE_FEATURES(sl) \
  108. (((struct w1_therm_family_data *)(sl->family_data))->features)
  109. /*
  110. * return whether or not a converT command has been issued to the slave
  111. * * 0: no bulk read is pending
  112. * * -1: conversion is in progress
  113. * * 1: conversion done, result to be read
  114. */
  115. #define SLAVE_CONVERT_TRIGGERED(sl) \
  116. (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered)
  117. /* return the address of the refcnt in the family data */
  118. #define THERM_REFCNT(family_data) \
  119. (&((struct w1_therm_family_data *)family_data)->refcnt)
  120. /* Structs definition */
  121. /**
  122. * struct w1_therm_family_converter - bind device specific functions
  123. * @broken: flag for non-registred families
  124. * @reserved: not used here
  125. * @f: pointer to the device binding structure
  126. * @convert: pointer to the device conversion function
  127. * @get_conversion_time: pointer to the device conversion time function
  128. * @set_resolution: pointer to the device set_resolution function
  129. * @get_resolution: pointer to the device get_resolution function
  130. * @write_data: pointer to the device writing function (2 or 3 bytes)
  131. * @bulk_read: true if device family support bulk read, false otherwise
  132. */
  133. struct w1_therm_family_converter {
  134. u8 broken;
  135. u16 reserved;
  136. struct w1_family *f;
  137. int (*convert)(u8 rom[9]);
  138. int (*get_conversion_time)(struct w1_slave *sl);
  139. int (*set_resolution)(struct w1_slave *sl, int val);
  140. int (*get_resolution)(struct w1_slave *sl);
  141. int (*write_data)(struct w1_slave *sl, const u8 *data);
  142. bool bulk_read;
  143. };
  144. /**
  145. * struct w1_therm_family_data - device data
  146. * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte)
  147. * @refcnt: ref count
  148. * @external_powered: 1 device powered externally,
  149. * 0 device parasite powered,
  150. * -x error or undefined
  151. * @resolution: current device resolution
  152. * @convert_triggered: conversion state of the device
  153. * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT
  154. * @features: bit mask - enable temperature validity check, poll for completion
  155. * @specific_functions: pointer to struct of device specific function
  156. */
  157. struct w1_therm_family_data {
  158. uint8_t rom[9];
  159. atomic_t refcnt;
  160. int external_powered;
  161. int resolution;
  162. int convert_triggered;
  163. int conv_time_override;
  164. unsigned int features;
  165. struct w1_therm_family_converter *specific_functions;
  166. };
  167. /**
  168. * struct therm_info - store temperature reading
  169. * @rom: read device data (8 data bytes + 1 CRC byte)
  170. * @crc: computed crc from rom
  171. * @verdict: 1 crc checked, 0 crc not matching
  172. */
  173. struct therm_info {
  174. u8 rom[9];
  175. u8 crc;
  176. u8 verdict;
  177. };
  178. /* Hardware Functions declaration */
  179. /**
  180. * reset_select_slave() - reset and select a slave
  181. * @sl: the slave to select
  182. *
  183. * Resets the bus and select the slave by sending a ROM MATCH cmd
  184. * w1_reset_select_slave() from w1_io.c could not be used here because
  185. * it sent a SKIP ROM command if only one device is on the line.
  186. * At the beginning of the such process, sl->master->slave_count is 1 even if
  187. * more devices are on the line, causing collision on the line.
  188. *
  189. * Context: The w1 master lock must be held.
  190. *
  191. * Return: 0 if success, negative kernel error code otherwise.
  192. */
  193. static int reset_select_slave(struct w1_slave *sl);
  194. /**
  195. * convert_t() - Query the device for temperature conversion and read
  196. * @sl: pointer to the slave to read
  197. * @info: pointer to a structure to store the read results
  198. *
  199. * Return: 0 if success, -kernel error code otherwise
  200. */
  201. static int convert_t(struct w1_slave *sl, struct therm_info *info);
  202. /**
  203. * read_scratchpad() - read the data in device RAM
  204. * @sl: pointer to the slave to read
  205. * @info: pointer to a structure to store the read results
  206. *
  207. * Return: 0 if success, -kernel error code otherwise
  208. */
  209. static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
  210. /**
  211. * write_scratchpad() - write nb_bytes in the device RAM
  212. * @sl: pointer to the slave to write in
  213. * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written
  214. * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise)
  215. *
  216. * Return: 0 if success, -kernel error code otherwise
  217. */
  218. static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
  219. /**
  220. * copy_scratchpad() - Copy the content of scratchpad in device EEPROM
  221. * @sl: slave involved
  222. *
  223. * Return: 0 if success, -kernel error code otherwise
  224. */
  225. static int copy_scratchpad(struct w1_slave *sl);
  226. /**
  227. * recall_eeprom() - Restore EEPROM data to device RAM
  228. * @sl: slave involved
  229. *
  230. * Return: 0 if success, -kernel error code otherwise
  231. */
  232. static int recall_eeprom(struct w1_slave *sl);
  233. /**
  234. * read_powermode() - Query the power mode of the slave
  235. * @sl: slave to retrieve the power mode
  236. *
  237. * Ask the device to get its power mode (external or parasite)
  238. * and store the power status in the &struct w1_therm_family_data.
  239. *
  240. * Return:
  241. * * 0 parasite powered device
  242. * * 1 externally powered device
  243. * * <0 kernel error code
  244. */
  245. static int read_powermode(struct w1_slave *sl);
  246. /**
  247. * trigger_bulk_read() - function to trigger a bulk read on the bus
  248. * @dev_master: the device master of the bus
  249. *
  250. * Send a SKIP ROM follow by a CONVERT T commmand on the bus.
  251. * It also set the status flag in each slave &struct w1_therm_family_data
  252. * to signal that a conversion is in progress.
  253. *
  254. * Return: 0 if success, -kernel error code otherwise
  255. */
  256. static int trigger_bulk_read(struct w1_master *dev_master);
  257. /* Sysfs interface declaration */
  258. static ssize_t w1_slave_show(struct device *device,
  259. struct device_attribute *attr, char *buf);
  260. static ssize_t w1_slave_store(struct device *device,
  261. struct device_attribute *attr, const char *buf, size_t size);
  262. static ssize_t w1_seq_show(struct device *device,
  263. struct device_attribute *attr, char *buf);
  264. static ssize_t temperature_show(struct device *device,
  265. struct device_attribute *attr, char *buf);
  266. static ssize_t ext_power_show(struct device *device,
  267. struct device_attribute *attr, char *buf);
  268. static ssize_t resolution_show(struct device *device,
  269. struct device_attribute *attr, char *buf);
  270. static ssize_t resolution_store(struct device *device,
  271. struct device_attribute *attr, const char *buf, size_t size);
  272. static ssize_t eeprom_cmd_store(struct device *device,
  273. struct device_attribute *attr, const char *buf, size_t size);
  274. static ssize_t alarms_store(struct device *device,
  275. struct device_attribute *attr, const char *buf, size_t size);
  276. static ssize_t alarms_show(struct device *device,
  277. struct device_attribute *attr, char *buf);
  278. static ssize_t therm_bulk_read_store(struct device *device,
  279. struct device_attribute *attr, const char *buf, size_t size);
  280. static ssize_t therm_bulk_read_show(struct device *device,
  281. struct device_attribute *attr, char *buf);
  282. static ssize_t conv_time_show(struct device *device,
  283. struct device_attribute *attr, char *buf);
  284. static ssize_t conv_time_store(struct device *device,
  285. struct device_attribute *attr, const char *buf,
  286. size_t size);
  287. static ssize_t features_show(struct device *device,
  288. struct device_attribute *attr, char *buf);
  289. static ssize_t features_store(struct device *device,
  290. struct device_attribute *attr, const char *buf,
  291. size_t size);
  292. /* Attributes declarations */
  293. static DEVICE_ATTR_RW(w1_slave);
  294. static DEVICE_ATTR_RO(w1_seq);
  295. static DEVICE_ATTR_RO(temperature);
  296. static DEVICE_ATTR_RO(ext_power);
  297. static DEVICE_ATTR_RW(resolution);
  298. static DEVICE_ATTR_WO(eeprom_cmd);
  299. static DEVICE_ATTR_RW(alarms);
  300. static DEVICE_ATTR_RW(conv_time);
  301. static DEVICE_ATTR_RW(features);
  302. static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */
  303. /* Interface Functions declaration */
  304. /**
  305. * w1_therm_add_slave() - Called when a new slave is discovered
  306. * @sl: slave just discovered by the master.
  307. *
  308. * Called by the master when the slave is discovered on the bus. Used to
  309. * initialize slave state before the beginning of any communication.
  310. *
  311. * Return: 0 - If success, negative kernel code otherwise
  312. */
  313. static int w1_therm_add_slave(struct w1_slave *sl);
  314. /**
  315. * w1_therm_remove_slave() - Called when a slave is removed
  316. * @sl: slave to be removed.
  317. *
  318. * Called by the master when the slave is considered not to be on the bus
  319. * anymore. Used to free memory.
  320. */
  321. static void w1_therm_remove_slave(struct w1_slave *sl);
  322. /* Family attributes */
  323. static struct attribute *w1_therm_attrs[] = {
  324. &dev_attr_w1_slave.attr,
  325. &dev_attr_temperature.attr,
  326. &dev_attr_ext_power.attr,
  327. &dev_attr_resolution.attr,
  328. &dev_attr_eeprom_cmd.attr,
  329. &dev_attr_alarms.attr,
  330. &dev_attr_conv_time.attr,
  331. &dev_attr_features.attr,
  332. NULL,
  333. };
  334. static struct attribute *w1_ds18s20_attrs[] = {
  335. &dev_attr_w1_slave.attr,
  336. &dev_attr_temperature.attr,
  337. &dev_attr_ext_power.attr,
  338. &dev_attr_eeprom_cmd.attr,
  339. &dev_attr_alarms.attr,
  340. &dev_attr_conv_time.attr,
  341. &dev_attr_features.attr,
  342. NULL,
  343. };
  344. static struct attribute *w1_ds28ea00_attrs[] = {
  345. &dev_attr_w1_slave.attr,
  346. &dev_attr_w1_seq.attr,
  347. &dev_attr_temperature.attr,
  348. &dev_attr_ext_power.attr,
  349. &dev_attr_resolution.attr,
  350. &dev_attr_eeprom_cmd.attr,
  351. &dev_attr_alarms.attr,
  352. &dev_attr_conv_time.attr,
  353. &dev_attr_features.attr,
  354. NULL,
  355. };
  356. /* Attribute groups */
  357. ATTRIBUTE_GROUPS(w1_therm);
  358. ATTRIBUTE_GROUPS(w1_ds18s20);
  359. ATTRIBUTE_GROUPS(w1_ds28ea00);
  360. #if IS_REACHABLE(CONFIG_HWMON)
  361. static int w1_read_temp(struct device *dev, u32 attr, int channel,
  362. long *val);
  363. static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
  364. u32 attr, int channel)
  365. {
  366. return attr == hwmon_temp_input ? 0444 : 0;
  367. }
  368. static int w1_read(struct device *dev, enum hwmon_sensor_types type,
  369. u32 attr, int channel, long *val)
  370. {
  371. switch (type) {
  372. case hwmon_temp:
  373. return w1_read_temp(dev, attr, channel, val);
  374. default:
  375. return -EOPNOTSUPP;
  376. }
  377. }
  378. static const u32 w1_temp_config[] = {
  379. HWMON_T_INPUT,
  380. 0
  381. };
  382. static const struct hwmon_channel_info w1_temp = {
  383. .type = hwmon_temp,
  384. .config = w1_temp_config,
  385. };
  386. static const struct hwmon_channel_info *w1_info[] = {
  387. &w1_temp,
  388. NULL
  389. };
  390. static const struct hwmon_ops w1_hwmon_ops = {
  391. .is_visible = w1_is_visible,
  392. .read = w1_read,
  393. };
  394. static const struct hwmon_chip_info w1_chip_info = {
  395. .ops = &w1_hwmon_ops,
  396. .info = w1_info,
  397. };
  398. #define W1_CHIPINFO (&w1_chip_info)
  399. #else
  400. #define W1_CHIPINFO NULL
  401. #endif
  402. /* Family operations */
  403. static const struct w1_family_ops w1_therm_fops = {
  404. .add_slave = w1_therm_add_slave,
  405. .remove_slave = w1_therm_remove_slave,
  406. .groups = w1_therm_groups,
  407. .chip_info = W1_CHIPINFO,
  408. };
  409. static const struct w1_family_ops w1_ds18s20_fops = {
  410. .add_slave = w1_therm_add_slave,
  411. .remove_slave = w1_therm_remove_slave,
  412. .groups = w1_ds18s20_groups,
  413. .chip_info = W1_CHIPINFO,
  414. };
  415. static const struct w1_family_ops w1_ds28ea00_fops = {
  416. .add_slave = w1_therm_add_slave,
  417. .remove_slave = w1_therm_remove_slave,
  418. .groups = w1_ds28ea00_groups,
  419. .chip_info = W1_CHIPINFO,
  420. };
  421. /* Family binding operations struct */
  422. static struct w1_family w1_therm_family_DS18S20 = {
  423. .fid = W1_THERM_DS18S20,
  424. .fops = &w1_ds18s20_fops,
  425. };
  426. static struct w1_family w1_therm_family_DS18B20 = {
  427. .fid = W1_THERM_DS18B20,
  428. .fops = &w1_therm_fops,
  429. };
  430. static struct w1_family w1_therm_family_DS1822 = {
  431. .fid = W1_THERM_DS1822,
  432. .fops = &w1_therm_fops,
  433. };
  434. static struct w1_family w1_therm_family_DS28EA00 = {
  435. .fid = W1_THERM_DS28EA00,
  436. .fops = &w1_ds28ea00_fops,
  437. };
  438. static struct w1_family w1_therm_family_DS1825 = {
  439. .fid = W1_THERM_DS1825,
  440. .fops = &w1_therm_fops,
  441. };
  442. /* Device dependent func */
  443. static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
  444. {
  445. int ret;
  446. if (!sl->family_data)
  447. return -ENODEV; /* device unknown */
  448. if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
  449. return SLAVE_CONV_TIME_OVERRIDE(sl);
  450. /* Return the conversion time, depending on resolution,
  451. * select maximum conversion time among all compatible devices
  452. */
  453. switch (SLAVE_RESOLUTION(sl)) {
  454. case 9:
  455. ret = 95;
  456. break;
  457. case 10:
  458. ret = 190;
  459. break;
  460. case 11:
  461. ret = 375;
  462. break;
  463. case 12:
  464. ret = 750;
  465. break;
  466. case 13:
  467. ret = 850; /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */
  468. break;
  469. case 14:
  470. ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */
  471. break;
  472. default:
  473. ret = 750;
  474. }
  475. return ret;
  476. }
  477. static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
  478. {
  479. if (!sl->family_data)
  480. return -ENODEV; /* device unknown */
  481. if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT)
  482. return 750; /* default for DS18S20 */
  483. else
  484. return SLAVE_CONV_TIME_OVERRIDE(sl);
  485. }
  486. static inline int w1_DS1825_convert_time(struct w1_slave *sl)
  487. {
  488. int ret;
  489. if (!sl->family_data)
  490. return -ENODEV; /* device unknown */
  491. if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
  492. return SLAVE_CONV_TIME_OVERRIDE(sl);
  493. /* Return the conversion time, depending on resolution,
  494. * select maximum conversion time among all compatible devices
  495. */
  496. switch (SLAVE_RESOLUTION(sl)) {
  497. case 9:
  498. ret = 95;
  499. break;
  500. case 10:
  501. ret = 190;
  502. break;
  503. case 11:
  504. ret = 375;
  505. break;
  506. case 12:
  507. ret = 750;
  508. break;
  509. case 14:
  510. ret = 100; /* MAX31850 only. Datasheet says 100ms */
  511. break;
  512. default:
  513. ret = 750;
  514. }
  515. return ret;
  516. }
  517. static inline int w1_DS18B20_write_data(struct w1_slave *sl,
  518. const u8 *data)
  519. {
  520. return write_scratchpad(sl, data, 3);
  521. }
  522. static inline int w1_DS18S20_write_data(struct w1_slave *sl,
  523. const u8 *data)
  524. {
  525. /* No config register */
  526. return write_scratchpad(sl, data, 2);
  527. }
  528. static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val)
  529. {
  530. int ret;
  531. struct therm_info info, info2;
  532. /* DS18B20 resolution is 9 to 12 bits */
  533. /* GX20MH01 resolution is 9 to 14 bits */
  534. /* MAX31850 resolution is fixed 14 bits */
  535. if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX)
  536. return -EINVAL;
  537. /* Calc bit value from resolution */
  538. val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT;
  539. /*
  540. * Read the scratchpad to change only the required bits
  541. * (bit5 & bit 6 from byte 4)
  542. */
  543. ret = read_scratchpad(sl, &info);
  544. if (ret)
  545. return ret;
  546. info.rom[4] &= ~W1_THERM_RESOLUTION_MASK;
  547. info.rom[4] |= val;
  548. /* Write data in the device RAM */
  549. ret = w1_DS18B20_write_data(sl, info.rom + 2);
  550. if (ret)
  551. return ret;
  552. /* Have to read back the resolution to verify an actual value
  553. * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ
  554. * Some DS18B20 clones don't support resolution change
  555. */
  556. ret = read_scratchpad(sl, &info2);
  557. if (ret)
  558. /* Scratchpad read fail */
  559. return ret;
  560. if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK))
  561. return 0;
  562. /* Resolution verify error */
  563. return -EIO;
  564. }
  565. static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
  566. {
  567. int ret;
  568. int resolution;
  569. struct therm_info info;
  570. ret = read_scratchpad(sl, &info);
  571. if (ret)
  572. return ret;
  573. resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT)
  574. + W1_THERM_RESOLUTION_MIN;
  575. /* GX20MH01 has one special case:
  576. * >=14 means 14 bits when getting resolution from bit value.
  577. * MAX31850 delivers fixed 15 and has 14 bits.
  578. * Other devices have no more then 12 bits.
  579. */
  580. if (resolution > W1_THERM_RESOLUTION_MAX)
  581. resolution = W1_THERM_RESOLUTION_MAX;
  582. return resolution;
  583. }
  584. /**
  585. * w1_DS18B20_convert_temp() - temperature computation for DS18B20
  586. * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
  587. *
  588. * Can be called for any DS18B20 compliant device.
  589. *
  590. * Return: value in millidegrees Celsius.
  591. */
  592. static inline int w1_DS18B20_convert_temp(u8 rom[9])
  593. {
  594. u16 bv;
  595. s16 t;
  596. /* Signed 16-bit value to unsigned, cpu order */
  597. bv = le16_to_cpup((__le16 *)rom);
  598. /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
  599. if (rom[4] & 0x80) {
  600. /* Insert two temperature bits from config register */
  601. /* Avoid arithmetic shift of signed value */
  602. bv = (bv << 2) | (rom[4] & 3);
  603. t = (s16) bv; /* Degrees, lowest bit is 2^-6 */
  604. return (int)t * 1000 / 64; /* Sign-extend to int; millidegrees */
  605. }
  606. t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
  607. return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
  608. }
  609. /**
  610. * w1_DS18S20_convert_temp() - temperature computation for DS18S20
  611. * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
  612. *
  613. * Can be called for any DS18S20 compliant device.
  614. *
  615. * Return: value in millidegrees Celsius.
  616. */
  617. static inline int w1_DS18S20_convert_temp(u8 rom[9])
  618. {
  619. int t, h;
  620. if (!rom[7]) {
  621. pr_debug("%s: Invalid argument for conversion\n", __func__);
  622. return 0;
  623. }
  624. if (rom[1] == 0)
  625. t = ((s32)rom[0] >> 1)*1000;
  626. else
  627. t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
  628. t -= 250;
  629. h = 1000*((s32)rom[7] - (s32)rom[6]);
  630. h /= (s32)rom[7];
  631. t += h;
  632. return t;
  633. }
  634. /**
  635. * w1_DS1825_convert_temp() - temperature computation for DS1825
  636. * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
  637. *
  638. * Can be called for any DS1825 compliant device.
  639. * Is used by MAX31850, too
  640. *
  641. * Return: value in millidegrees Celsius.
  642. */
  643. static inline int w1_DS1825_convert_temp(u8 rom[9])
  644. {
  645. u16 bv;
  646. s16 t;
  647. /* Signed 16-bit value to unsigned, cpu order */
  648. bv = le16_to_cpup((__le16 *)rom);
  649. /* Config register bit 7 = 1 - MA31850 found, 14 bit resolution */
  650. if (rom[4] & 0x80) {
  651. /* Mask out bits 0 (Fault) and 1 (Reserved) */
  652. /* Avoid arithmetic shift of signed value */
  653. bv = (bv & 0xFFFC); /* Degrees, lowest 4 bits are 2^-1, 2^-2 and 2 zero bits */
  654. }
  655. t = (s16)bv; /* Degrees, lowest bit is 2^-4 */
  656. return (int)t * 1000 / 16; /* Sign-extend to int; millidegrees */
  657. }
  658. /* Device capability description */
  659. /* GX20MH01 device shares family number and structure with DS18B20 */
  660. static struct w1_therm_family_converter w1_therm_families[] = {
  661. {
  662. .f = &w1_therm_family_DS18S20,
  663. .convert = w1_DS18S20_convert_temp,
  664. .get_conversion_time = w1_DS18S20_convert_time,
  665. .set_resolution = NULL, /* no config register */
  666. .get_resolution = NULL, /* no config register */
  667. .write_data = w1_DS18S20_write_data,
  668. .bulk_read = true
  669. },
  670. {
  671. .f = &w1_therm_family_DS1822,
  672. .convert = w1_DS18B20_convert_temp,
  673. .get_conversion_time = w1_DS18B20_convert_time,
  674. .set_resolution = w1_DS18B20_set_resolution,
  675. .get_resolution = w1_DS18B20_get_resolution,
  676. .write_data = w1_DS18B20_write_data,
  677. .bulk_read = true
  678. },
  679. {
  680. /* Also used for GX20MH01 */
  681. .f = &w1_therm_family_DS18B20,
  682. .convert = w1_DS18B20_convert_temp,
  683. .get_conversion_time = w1_DS18B20_convert_time,
  684. .set_resolution = w1_DS18B20_set_resolution,
  685. .get_resolution = w1_DS18B20_get_resolution,
  686. .write_data = w1_DS18B20_write_data,
  687. .bulk_read = true
  688. },
  689. {
  690. .f = &w1_therm_family_DS28EA00,
  691. .convert = w1_DS18B20_convert_temp,
  692. .get_conversion_time = w1_DS18B20_convert_time,
  693. .set_resolution = w1_DS18B20_set_resolution,
  694. .get_resolution = w1_DS18B20_get_resolution,
  695. .write_data = w1_DS18B20_write_data,
  696. .bulk_read = false
  697. },
  698. {
  699. /* Also used for MAX31850 */
  700. .f = &w1_therm_family_DS1825,
  701. .convert = w1_DS1825_convert_temp,
  702. .get_conversion_time = w1_DS1825_convert_time,
  703. .set_resolution = w1_DS18B20_set_resolution,
  704. .get_resolution = w1_DS18B20_get_resolution,
  705. .write_data = w1_DS18B20_write_data,
  706. .bulk_read = true
  707. }
  708. };
  709. /* Helpers Functions */
  710. /**
  711. * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
  712. * @sl: slave to retrieve the device specific structure
  713. *
  714. * Return: pointer to the slaves's family converter, NULL if not known
  715. */
  716. static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
  717. {
  718. struct w1_therm_family_converter *ret = NULL;
  719. int i;
  720. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
  721. if (w1_therm_families[i].f->fid == sl->family->fid) {
  722. ret = &w1_therm_families[i];
  723. break;
  724. }
  725. }
  726. return ret;
  727. }
  728. /**
  729. * bus_mutex_lock() - Acquire the mutex
  730. * @lock: w1 bus mutex to acquire
  731. *
  732. * It try to acquire the mutex W1_THERM_MAX_TRY times and wait
  733. * W1_THERM_RETRY_DELAY between 2 attempts.
  734. *
  735. * Return: true is mutex is acquired and lock, false otherwise
  736. */
  737. static inline bool bus_mutex_lock(struct mutex *lock)
  738. {
  739. int max_trying = W1_THERM_MAX_TRY;
  740. /* try to acquire the mutex, if not, sleep retry_delay before retry) */
  741. while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) {
  742. unsigned long sleep_rem;
  743. sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY);
  744. if (!sleep_rem)
  745. max_trying--;
  746. }
  747. if (!max_trying)
  748. return false; /* Didn't acquire the bus mutex */
  749. return true;
  750. }
  751. /**
  752. * check_family_data() - Check if family data and specific functions are present
  753. * @sl: W1 device data
  754. *
  755. * Return: 0 - OK, negative value - error
  756. */
  757. static int check_family_data(struct w1_slave *sl)
  758. {
  759. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  760. dev_info(&sl->dev,
  761. "%s: Device is not supported by the driver\n", __func__);
  762. return -EINVAL; /* No device family */
  763. }
  764. return 0;
  765. }
  766. /**
  767. * bulk_read_support() - check if slave support bulk read
  768. * @sl: device to check the ability
  769. *
  770. * Return: true if bulk read is supported, false if not or error
  771. */
  772. static inline bool bulk_read_support(struct w1_slave *sl)
  773. {
  774. if (SLAVE_SPECIFIC_FUNC(sl))
  775. return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
  776. dev_info(&sl->dev,
  777. "%s: Device not supported by the driver\n", __func__);
  778. return false; /* No device family */
  779. }
  780. /**
  781. * conversion_time() - get the Tconv for the slave
  782. * @sl: device to get the conversion time
  783. *
  784. * On device supporting resolution settings, conversion time depend
  785. * on the resolution setting. This helper function get the slave timing,
  786. * depending on its current setting.
  787. *
  788. * Return: conversion time in ms, negative values are kernel error code
  789. */
  790. static inline int conversion_time(struct w1_slave *sl)
  791. {
  792. if (SLAVE_SPECIFIC_FUNC(sl))
  793. return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
  794. dev_info(&sl->dev,
  795. "%s: Device not supported by the driver\n", __func__);
  796. return -ENODEV; /* No device family */
  797. }
  798. /**
  799. * temperature_from_RAM() - Convert the read info to temperature
  800. * @sl: device that sent the RAM data
  801. * @rom: read value on the slave device RAM
  802. *
  803. * Device dependent, the function bind the correct computation method.
  804. *
  805. * Return: temperature in 1/1000degC, 0 on error.
  806. */
  807. static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
  808. {
  809. if (SLAVE_SPECIFIC_FUNC(sl))
  810. return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
  811. dev_info(&sl->dev,
  812. "%s: Device not supported by the driver\n", __func__);
  813. return 0; /* No device family */
  814. }
  815. /**
  816. * int_to_short() - Safe casting of int to short
  817. *
  818. * @i: integer to be converted to short
  819. *
  820. * Device register use 1 byte to store signed integer.
  821. * This helper function convert the int in a signed short,
  822. * using the min/max values that device can measure as limits.
  823. * min/max values are defined by macro.
  824. *
  825. * Return: a short in the range of min/max value
  826. */
  827. static inline s8 int_to_short(int i)
  828. {
  829. /* Prepare to cast to short by eliminating out of range values */
  830. i = clamp(i, MIN_TEMP, MAX_TEMP);
  831. return (s8) i;
  832. }
  833. /* Interface Functions */
  834. static int w1_therm_add_slave(struct w1_slave *sl)
  835. {
  836. struct w1_therm_family_converter *sl_family_conv;
  837. /* Allocate memory */
  838. sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
  839. GFP_KERNEL);
  840. if (!sl->family_data)
  841. return -ENOMEM;
  842. atomic_set(THERM_REFCNT(sl->family_data), 1);
  843. /* Get a pointer to the device specific function struct */
  844. sl_family_conv = device_family(sl);
  845. if (!sl_family_conv) {
  846. kfree(sl->family_data);
  847. return -ENODEV;
  848. }
  849. /* save this pointer to the device structure */
  850. SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
  851. if (bulk_read_support(sl)) {
  852. /*
  853. * add the sys entry to trigger bulk_read
  854. * at master level only the 1st time
  855. */
  856. if (!bulk_read_device_counter) {
  857. int err = device_create_file(&sl->master->dev,
  858. &dev_attr_therm_bulk_read);
  859. if (err)
  860. dev_warn(&sl->dev,
  861. "%s: Device has been added, but bulk read is unavailable. err=%d\n",
  862. __func__, err);
  863. }
  864. /* Increment the counter */
  865. bulk_read_device_counter++;
  866. }
  867. /* Getting the power mode of the device {external, parasite} */
  868. SLAVE_POWERMODE(sl) = read_powermode(sl);
  869. if (SLAVE_POWERMODE(sl) < 0) {
  870. /* no error returned as device has been added */
  871. dev_warn(&sl->dev,
  872. "%s: Device has been added, but power_mode may be corrupted. err=%d\n",
  873. __func__, SLAVE_POWERMODE(sl));
  874. }
  875. /* Getting the resolution of the device */
  876. if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
  877. SLAVE_RESOLUTION(sl) =
  878. SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
  879. if (SLAVE_RESOLUTION(sl) < 0) {
  880. /* no error returned as device has been added */
  881. dev_warn(&sl->dev,
  882. "%s:Device has been added, but resolution may be corrupted. err=%d\n",
  883. __func__, SLAVE_RESOLUTION(sl));
  884. }
  885. }
  886. /* Finally initialize convert_triggered flag */
  887. SLAVE_CONVERT_TRIGGERED(sl) = 0;
  888. return 0;
  889. }
  890. static void w1_therm_remove_slave(struct w1_slave *sl)
  891. {
  892. int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
  893. if (bulk_read_support(sl)) {
  894. bulk_read_device_counter--;
  895. /* Delete the entry if no more device support the feature */
  896. if (!bulk_read_device_counter)
  897. device_remove_file(&sl->master->dev,
  898. &dev_attr_therm_bulk_read);
  899. }
  900. while (refcnt) {
  901. msleep(1000);
  902. refcnt = atomic_read(THERM_REFCNT(sl->family_data));
  903. }
  904. kfree(sl->family_data);
  905. sl->family_data = NULL;
  906. }
  907. /* Hardware Functions */
  908. /* Safe version of reset_select_slave - avoid using the one in w_io.c */
  909. static int reset_select_slave(struct w1_slave *sl)
  910. {
  911. u8 match[9] = { W1_MATCH_ROM, };
  912. u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
  913. if (w1_reset_bus(sl->master))
  914. return -ENODEV;
  915. memcpy(&match[1], &rn, 8);
  916. w1_write_block(sl->master, match, 9);
  917. return 0;
  918. }
  919. /**
  920. * w1_poll_completion - Poll for operation completion, with timeout
  921. * @dev_master: the device master of the bus
  922. * @tout_ms: timeout in milliseconds
  923. *
  924. * The device is answering 0's while an operation is in progress and 1's after it completes
  925. * Timeout may happen if the previous command was not recognised due to a line noise
  926. *
  927. * Return: 0 - OK, negative error - timeout
  928. */
  929. static int w1_poll_completion(struct w1_master *dev_master, int tout_ms)
  930. {
  931. int i;
  932. for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) {
  933. /* Delay is before poll, for device to recognize a command */
  934. msleep(W1_POLL_PERIOD);
  935. /* Compare all 8 bits to mitigate a noise on the bus */
  936. if (w1_read_8(dev_master) == 0xFF)
  937. break;
  938. }
  939. if (i == tout_ms/W1_POLL_PERIOD)
  940. return -EIO;
  941. return 0;
  942. }
  943. static int convert_t(struct w1_slave *sl, struct therm_info *info)
  944. {
  945. struct w1_master *dev_master = sl->master;
  946. int max_trying = W1_THERM_MAX_TRY;
  947. int t_conv;
  948. int ret = -ENODEV;
  949. bool strong_pullup;
  950. if (!sl->family_data)
  951. goto error;
  952. strong_pullup = (w1_strong_pullup == 2 ||
  953. (!SLAVE_POWERMODE(sl) &&
  954. w1_strong_pullup));
  955. if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
  956. dev_warn(&sl->dev,
  957. "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n",
  958. __func__);
  959. SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
  960. }
  961. /* get conversion duration device and id dependent */
  962. t_conv = conversion_time(sl);
  963. memset(info->rom, 0, sizeof(info->rom));
  964. /* prevent the slave from going away in sleep */
  965. atomic_inc(THERM_REFCNT(sl->family_data));
  966. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  967. ret = -EAGAIN; /* Didn't acquire the mutex */
  968. goto dec_refcnt;
  969. }
  970. while (max_trying-- && ret) { /* ret should be 0 */
  971. info->verdict = 0;
  972. info->crc = 0;
  973. /* safe version to select slave */
  974. if (!reset_select_slave(sl)) {
  975. unsigned long sleep_rem;
  976. /* 750ms strong pullup (or delay) after the convert */
  977. if (strong_pullup)
  978. w1_next_pullup(dev_master, t_conv);
  979. w1_write_8(dev_master, W1_CONVERT_TEMP);
  980. if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
  981. ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
  982. if (ret) {
  983. dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
  984. goto mt_unlock;
  985. }
  986. mutex_unlock(&dev_master->bus_mutex);
  987. } else if (!strong_pullup) { /*no device need pullup */
  988. sleep_rem = msleep_interruptible(t_conv);
  989. if (sleep_rem != 0) {
  990. ret = -EINTR;
  991. goto mt_unlock;
  992. }
  993. mutex_unlock(&dev_master->bus_mutex);
  994. } else { /*some device need pullup */
  995. mutex_unlock(&dev_master->bus_mutex);
  996. sleep_rem = msleep_interruptible(t_conv);
  997. if (sleep_rem != 0) {
  998. ret = -EINTR;
  999. goto dec_refcnt;
  1000. }
  1001. }
  1002. ret = read_scratchpad(sl, info);
  1003. /* If enabled, check for conversion success */
  1004. if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) &&
  1005. (info->rom[6] == 0xC) &&
  1006. ((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
  1007. (info->rom[1] == 0x7 && info->rom[0] == 0xFF))
  1008. ) {
  1009. /* Invalid reading (scratchpad byte 6 = 0xC)
  1010. * due to insufficient conversion time
  1011. * or power failure.
  1012. */
  1013. ret = -EIO;
  1014. }
  1015. goto dec_refcnt;
  1016. }
  1017. }
  1018. mt_unlock:
  1019. mutex_unlock(&dev_master->bus_mutex);
  1020. dec_refcnt:
  1021. atomic_dec(THERM_REFCNT(sl->family_data));
  1022. error:
  1023. return ret;
  1024. }
  1025. static int conv_time_measure(struct w1_slave *sl, int *conv_time)
  1026. {
  1027. struct therm_info inf,
  1028. *info = &inf;
  1029. struct w1_master *dev_master = sl->master;
  1030. int max_trying = W1_THERM_MAX_TRY;
  1031. int ret = -ENODEV;
  1032. bool strong_pullup;
  1033. if (!sl->family_data)
  1034. goto error;
  1035. strong_pullup = (w1_strong_pullup == 2 ||
  1036. (!SLAVE_POWERMODE(sl) &&
  1037. w1_strong_pullup));
  1038. if (strong_pullup) {
  1039. pr_info("%s: Measure with strong_pullup is not supported.\n", __func__);
  1040. return -EINVAL;
  1041. }
  1042. memset(info->rom, 0, sizeof(info->rom));
  1043. /* prevent the slave from going away in sleep */
  1044. atomic_inc(THERM_REFCNT(sl->family_data));
  1045. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1046. ret = -EAGAIN; /* Didn't acquire the mutex */
  1047. goto dec_refcnt;
  1048. }
  1049. while (max_trying-- && ret) { /* ret should be 0 */
  1050. info->verdict = 0;
  1051. info->crc = 0;
  1052. /* safe version to select slave */
  1053. if (!reset_select_slave(sl)) {
  1054. int j_start, j_end;
  1055. /*no device need pullup */
  1056. w1_write_8(dev_master, W1_CONVERT_TEMP);
  1057. j_start = jiffies;
  1058. ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
  1059. if (ret) {
  1060. dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
  1061. goto mt_unlock;
  1062. }
  1063. j_end = jiffies;
  1064. /* 1.2x increase for variation and changes over temperature range */
  1065. *conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
  1066. pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n",
  1067. *conv_time, HZ);
  1068. if (*conv_time <= CONV_TIME_MEASURE) {
  1069. ret = -EIO;
  1070. goto mt_unlock;
  1071. }
  1072. mutex_unlock(&dev_master->bus_mutex);
  1073. ret = read_scratchpad(sl, info);
  1074. goto dec_refcnt;
  1075. }
  1076. }
  1077. mt_unlock:
  1078. mutex_unlock(&dev_master->bus_mutex);
  1079. dec_refcnt:
  1080. atomic_dec(THERM_REFCNT(sl->family_data));
  1081. error:
  1082. return ret;
  1083. }
  1084. static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
  1085. {
  1086. struct w1_master *dev_master = sl->master;
  1087. int max_trying = W1_THERM_MAX_TRY;
  1088. int ret = -ENODEV;
  1089. info->verdict = 0;
  1090. if (!sl->family_data)
  1091. goto error;
  1092. memset(info->rom, 0, sizeof(info->rom));
  1093. /* prevent the slave from going away in sleep */
  1094. atomic_inc(THERM_REFCNT(sl->family_data));
  1095. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1096. ret = -EAGAIN; /* Didn't acquire the mutex */
  1097. goto dec_refcnt;
  1098. }
  1099. while (max_trying-- && ret) { /* ret should be 0 */
  1100. /* safe version to select slave */
  1101. if (!reset_select_slave(sl)) {
  1102. u8 nb_bytes_read;
  1103. w1_write_8(dev_master, W1_READ_SCRATCHPAD);
  1104. nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
  1105. if (nb_bytes_read != 9) {
  1106. dev_warn(&sl->dev,
  1107. "w1_read_block(): returned %u instead of 9.\n",
  1108. nb_bytes_read);
  1109. ret = -EIO;
  1110. }
  1111. info->crc = w1_calc_crc8(info->rom, 8);
  1112. if (info->rom[8] == info->crc) {
  1113. info->verdict = 1;
  1114. ret = 0;
  1115. } else
  1116. ret = -EIO; /* CRC not checked */
  1117. }
  1118. }
  1119. mutex_unlock(&dev_master->bus_mutex);
  1120. dec_refcnt:
  1121. atomic_dec(THERM_REFCNT(sl->family_data));
  1122. error:
  1123. return ret;
  1124. }
  1125. static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
  1126. {
  1127. struct w1_master *dev_master = sl->master;
  1128. int max_trying = W1_THERM_MAX_TRY;
  1129. int ret = -ENODEV;
  1130. if (!sl->family_data)
  1131. goto error;
  1132. /* prevent the slave from going away in sleep */
  1133. atomic_inc(THERM_REFCNT(sl->family_data));
  1134. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1135. ret = -EAGAIN; /* Didn't acquire the mutex */
  1136. goto dec_refcnt;
  1137. }
  1138. while (max_trying-- && ret) { /* ret should be 0 */
  1139. /* safe version to select slave */
  1140. if (!reset_select_slave(sl)) {
  1141. w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
  1142. w1_write_block(dev_master, data, nb_bytes);
  1143. ret = 0;
  1144. }
  1145. }
  1146. mutex_unlock(&dev_master->bus_mutex);
  1147. dec_refcnt:
  1148. atomic_dec(THERM_REFCNT(sl->family_data));
  1149. error:
  1150. return ret;
  1151. }
  1152. static int copy_scratchpad(struct w1_slave *sl)
  1153. {
  1154. struct w1_master *dev_master = sl->master;
  1155. int max_trying = W1_THERM_MAX_TRY;
  1156. int t_write, ret = -ENODEV;
  1157. bool strong_pullup;
  1158. if (!sl->family_data)
  1159. goto error;
  1160. t_write = W1_THERM_EEPROM_WRITE_DELAY;
  1161. strong_pullup = (w1_strong_pullup == 2 ||
  1162. (!SLAVE_POWERMODE(sl) &&
  1163. w1_strong_pullup));
  1164. /* prevent the slave from going away in sleep */
  1165. atomic_inc(THERM_REFCNT(sl->family_data));
  1166. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1167. ret = -EAGAIN; /* Didn't acquire the mutex */
  1168. goto dec_refcnt;
  1169. }
  1170. while (max_trying-- && ret) { /* ret should be 0 */
  1171. /* safe version to select slave */
  1172. if (!reset_select_slave(sl)) {
  1173. unsigned long sleep_rem;
  1174. /* 10ms strong pullup (or delay) after the convert */
  1175. if (strong_pullup)
  1176. w1_next_pullup(dev_master, t_write);
  1177. w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
  1178. if (strong_pullup) {
  1179. sleep_rem = msleep_interruptible(t_write);
  1180. if (sleep_rem != 0) {
  1181. ret = -EINTR;
  1182. goto mt_unlock;
  1183. }
  1184. }
  1185. ret = 0;
  1186. }
  1187. }
  1188. mt_unlock:
  1189. mutex_unlock(&dev_master->bus_mutex);
  1190. dec_refcnt:
  1191. atomic_dec(THERM_REFCNT(sl->family_data));
  1192. error:
  1193. return ret;
  1194. }
  1195. static int recall_eeprom(struct w1_slave *sl)
  1196. {
  1197. struct w1_master *dev_master = sl->master;
  1198. int max_trying = W1_THERM_MAX_TRY;
  1199. int ret = -ENODEV;
  1200. if (!sl->family_data)
  1201. goto error;
  1202. /* prevent the slave from going away in sleep */
  1203. atomic_inc(THERM_REFCNT(sl->family_data));
  1204. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1205. ret = -EAGAIN; /* Didn't acquire the mutex */
  1206. goto dec_refcnt;
  1207. }
  1208. while (max_trying-- && ret) { /* ret should be 0 */
  1209. /* safe version to select slave */
  1210. if (!reset_select_slave(sl)) {
  1211. w1_write_8(dev_master, W1_RECALL_EEPROM);
  1212. ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM);
  1213. }
  1214. }
  1215. mutex_unlock(&dev_master->bus_mutex);
  1216. dec_refcnt:
  1217. atomic_dec(THERM_REFCNT(sl->family_data));
  1218. error:
  1219. return ret;
  1220. }
  1221. static int read_powermode(struct w1_slave *sl)
  1222. {
  1223. struct w1_master *dev_master = sl->master;
  1224. int max_trying = W1_THERM_MAX_TRY;
  1225. int ret = -ENODEV;
  1226. if (!sl->family_data)
  1227. goto error;
  1228. /* prevent the slave from going away in sleep */
  1229. atomic_inc(THERM_REFCNT(sl->family_data));
  1230. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1231. ret = -EAGAIN; /* Didn't acquire the mutex */
  1232. goto dec_refcnt;
  1233. }
  1234. while ((max_trying--) && (ret < 0)) {
  1235. /* safe version to select slave */
  1236. if (!reset_select_slave(sl)) {
  1237. w1_write_8(dev_master, W1_READ_PSUPPLY);
  1238. /*
  1239. * Emit a read time slot and read only one bit,
  1240. * 1 is externally powered,
  1241. * 0 is parasite powered
  1242. */
  1243. ret = w1_touch_bit(dev_master, 1);
  1244. /* ret should be either 1 either 0 */
  1245. }
  1246. }
  1247. mutex_unlock(&dev_master->bus_mutex);
  1248. dec_refcnt:
  1249. atomic_dec(THERM_REFCNT(sl->family_data));
  1250. error:
  1251. return ret;
  1252. }
  1253. static int trigger_bulk_read(struct w1_master *dev_master)
  1254. {
  1255. struct w1_slave *sl = NULL; /* used to iterate through slaves */
  1256. int max_trying = W1_THERM_MAX_TRY;
  1257. int t_conv = 0;
  1258. int ret = -ENODEV;
  1259. bool strong_pullup = false;
  1260. /*
  1261. * Check whether there are parasite powered device on the bus,
  1262. * and compute duration of conversion for these devices
  1263. * so we can apply a strong pullup if required
  1264. */
  1265. list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
  1266. if (!sl->family_data)
  1267. goto error;
  1268. if (bulk_read_support(sl)) {
  1269. int t_cur = conversion_time(sl);
  1270. t_conv = t_cur > t_conv ? t_cur : t_conv;
  1271. strong_pullup = strong_pullup ||
  1272. (w1_strong_pullup == 2 ||
  1273. (!SLAVE_POWERMODE(sl) &&
  1274. w1_strong_pullup));
  1275. }
  1276. }
  1277. /*
  1278. * t_conv is the max conversion time required on the bus
  1279. * If its 0, no device support the bulk read feature
  1280. */
  1281. if (!t_conv)
  1282. goto error;
  1283. if (!bus_mutex_lock(&dev_master->bus_mutex)) {
  1284. ret = -EAGAIN; /* Didn't acquire the mutex */
  1285. goto error;
  1286. }
  1287. while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */
  1288. if (!w1_reset_bus(dev_master)) { /* Just reset the bus */
  1289. unsigned long sleep_rem;
  1290. w1_write_8(dev_master, W1_SKIP_ROM);
  1291. if (strong_pullup) /* Apply pullup if required */
  1292. w1_next_pullup(dev_master, t_conv);
  1293. w1_write_8(dev_master, W1_CONVERT_TEMP);
  1294. /* set a flag to instruct that converT pending */
  1295. list_for_each_entry(sl,
  1296. &dev_master->slist, w1_slave_entry) {
  1297. if (bulk_read_support(sl))
  1298. SLAVE_CONVERT_TRIGGERED(sl) = -1;
  1299. }
  1300. if (strong_pullup) { /* some device need pullup */
  1301. sleep_rem = msleep_interruptible(t_conv);
  1302. if (sleep_rem != 0) {
  1303. ret = -EINTR;
  1304. goto mt_unlock;
  1305. }
  1306. mutex_unlock(&dev_master->bus_mutex);
  1307. } else {
  1308. mutex_unlock(&dev_master->bus_mutex);
  1309. sleep_rem = msleep_interruptible(t_conv);
  1310. if (sleep_rem != 0) {
  1311. ret = -EINTR;
  1312. goto set_flag;
  1313. }
  1314. }
  1315. ret = 0;
  1316. goto set_flag;
  1317. }
  1318. }
  1319. mt_unlock:
  1320. mutex_unlock(&dev_master->bus_mutex);
  1321. set_flag:
  1322. /* set a flag to register convsersion is done */
  1323. list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
  1324. if (bulk_read_support(sl))
  1325. SLAVE_CONVERT_TRIGGERED(sl) = 1;
  1326. }
  1327. error:
  1328. return ret;
  1329. }
  1330. /* Sysfs Interface definition */
  1331. static ssize_t w1_slave_show(struct device *device,
  1332. struct device_attribute *attr, char *buf)
  1333. {
  1334. struct w1_slave *sl = dev_to_w1_slave(device);
  1335. struct therm_info info;
  1336. u8 *family_data = sl->family_data;
  1337. int ret, i;
  1338. ssize_t c = PAGE_SIZE;
  1339. if (bulk_read_support(sl)) {
  1340. if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
  1341. dev_dbg(device,
  1342. "%s: Conversion in progress, retry later\n",
  1343. __func__);
  1344. return 0;
  1345. } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
  1346. /* A bulk read has been issued, read the device RAM */
  1347. ret = read_scratchpad(sl, &info);
  1348. SLAVE_CONVERT_TRIGGERED(sl) = 0;
  1349. } else
  1350. ret = convert_t(sl, &info);
  1351. } else
  1352. ret = convert_t(sl, &info);
  1353. if (ret < 0) {
  1354. dev_dbg(device,
  1355. "%s: Temperature data may be corrupted. err=%d\n",
  1356. __func__, ret);
  1357. return 0;
  1358. }
  1359. for (i = 0; i < 9; ++i)
  1360. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
  1361. c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
  1362. info.crc, (info.verdict) ? "YES" : "NO");
  1363. if (info.verdict)
  1364. memcpy(family_data, info.rom, sizeof(info.rom));
  1365. else
  1366. dev_warn(device, "%s:Read failed CRC check\n", __func__);
  1367. for (i = 0; i < 9; ++i)
  1368. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
  1369. ((u8 *)family_data)[i]);
  1370. c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
  1371. temperature_from_RAM(sl, info.rom));
  1372. ret = PAGE_SIZE - c;
  1373. return ret;
  1374. }
  1375. static ssize_t w1_slave_store(struct device *device,
  1376. struct device_attribute *attr, const char *buf,
  1377. size_t size)
  1378. {
  1379. int val, ret = 0;
  1380. struct w1_slave *sl = dev_to_w1_slave(device);
  1381. ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
  1382. if (ret) { /* conversion error */
  1383. dev_info(device,
  1384. "%s: conversion error. err= %d\n", __func__, ret);
  1385. return size; /* return size to avoid call back again */
  1386. }
  1387. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1388. dev_info(device,
  1389. "%s: Device not supported by the driver\n", __func__);
  1390. return size; /* No device family */
  1391. }
  1392. if (val == 0) /* val=0 : trigger a EEPROM save */
  1393. ret = copy_scratchpad(sl);
  1394. else {
  1395. if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
  1396. ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
  1397. }
  1398. if (ret) {
  1399. dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
  1400. /* Propagate error to userspace */
  1401. return ret;
  1402. }
  1403. SLAVE_RESOLUTION(sl) = val;
  1404. /* Reset the conversion time to default - it depends on resolution */
  1405. SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
  1406. return size; /* always return size to avoid infinite calling */
  1407. }
  1408. static ssize_t temperature_show(struct device *device,
  1409. struct device_attribute *attr, char *buf)
  1410. {
  1411. struct w1_slave *sl = dev_to_w1_slave(device);
  1412. struct therm_info info;
  1413. int ret = 0;
  1414. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1415. dev_info(device,
  1416. "%s: Device not supported by the driver\n", __func__);
  1417. return 0; /* No device family */
  1418. }
  1419. if (bulk_read_support(sl)) {
  1420. if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
  1421. dev_dbg(device,
  1422. "%s: Conversion in progress, retry later\n",
  1423. __func__);
  1424. return 0;
  1425. } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
  1426. /* A bulk read has been issued, read the device RAM */
  1427. ret = read_scratchpad(sl, &info);
  1428. SLAVE_CONVERT_TRIGGERED(sl) = 0;
  1429. } else
  1430. ret = convert_t(sl, &info);
  1431. } else
  1432. ret = convert_t(sl, &info);
  1433. if (ret < 0) {
  1434. dev_dbg(device,
  1435. "%s: Temperature data may be corrupted. err=%d\n",
  1436. __func__, ret);
  1437. return 0;
  1438. }
  1439. return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
  1440. }
  1441. static ssize_t ext_power_show(struct device *device,
  1442. struct device_attribute *attr, char *buf)
  1443. {
  1444. struct w1_slave *sl = dev_to_w1_slave(device);
  1445. if (!sl->family_data) {
  1446. dev_info(device,
  1447. "%s: Device not supported by the driver\n", __func__);
  1448. return 0; /* No device family */
  1449. }
  1450. /* Getting the power mode of the device {external, parasite} */
  1451. SLAVE_POWERMODE(sl) = read_powermode(sl);
  1452. if (SLAVE_POWERMODE(sl) < 0) {
  1453. dev_dbg(device,
  1454. "%s: Power_mode may be corrupted. err=%d\n",
  1455. __func__, SLAVE_POWERMODE(sl));
  1456. }
  1457. return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
  1458. }
  1459. static ssize_t resolution_show(struct device *device,
  1460. struct device_attribute *attr, char *buf)
  1461. {
  1462. struct w1_slave *sl = dev_to_w1_slave(device);
  1463. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1464. dev_info(device,
  1465. "%s: Device not supported by the driver\n", __func__);
  1466. return 0; /* No device family */
  1467. }
  1468. /* get the correct function depending on the device */
  1469. SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
  1470. if (SLAVE_RESOLUTION(sl) < 0) {
  1471. dev_dbg(device,
  1472. "%s: Resolution may be corrupted. err=%d\n",
  1473. __func__, SLAVE_RESOLUTION(sl));
  1474. }
  1475. return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
  1476. }
  1477. static ssize_t resolution_store(struct device *device,
  1478. struct device_attribute *attr, const char *buf, size_t size)
  1479. {
  1480. struct w1_slave *sl = dev_to_w1_slave(device);
  1481. int val;
  1482. int ret = 0;
  1483. ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
  1484. if (ret) { /* conversion error */
  1485. dev_info(device,
  1486. "%s: conversion error. err= %d\n", __func__, ret);
  1487. return size; /* return size to avoid call back again */
  1488. }
  1489. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1490. dev_info(device,
  1491. "%s: Device not supported by the driver\n", __func__);
  1492. return size; /* No device family */
  1493. }
  1494. /*
  1495. * Don't deal with the val enterd by user,
  1496. * only device knows what is correct or not
  1497. */
  1498. /* get the correct function depending on the device */
  1499. ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
  1500. if (ret)
  1501. return ret;
  1502. SLAVE_RESOLUTION(sl) = val;
  1503. /* Reset the conversion time to default because it depends on resolution */
  1504. SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
  1505. return size;
  1506. }
  1507. static ssize_t eeprom_cmd_store(struct device *device,
  1508. struct device_attribute *attr, const char *buf, size_t size)
  1509. {
  1510. struct w1_slave *sl = dev_to_w1_slave(device);
  1511. int ret = -EINVAL; /* Invalid argument */
  1512. if (size == sizeof(EEPROM_CMD_WRITE)) {
  1513. if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
  1514. ret = copy_scratchpad(sl);
  1515. } else if (size == sizeof(EEPROM_CMD_READ)) {
  1516. if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
  1517. ret = recall_eeprom(sl);
  1518. }
  1519. if (ret)
  1520. dev_info(device, "%s: error in process %d\n", __func__, ret);
  1521. return size;
  1522. }
  1523. static ssize_t alarms_show(struct device *device,
  1524. struct device_attribute *attr, char *buf)
  1525. {
  1526. struct w1_slave *sl = dev_to_w1_slave(device);
  1527. int ret;
  1528. s8 th = 0, tl = 0;
  1529. struct therm_info scratchpad;
  1530. ret = read_scratchpad(sl, &scratchpad);
  1531. if (!ret) {
  1532. th = scratchpad.rom[2]; /* TH is byte 2 */
  1533. tl = scratchpad.rom[3]; /* TL is byte 3 */
  1534. } else {
  1535. dev_info(device,
  1536. "%s: error reading alarms register %d\n",
  1537. __func__, ret);
  1538. }
  1539. return sprintf(buf, "%hd %hd\n", tl, th);
  1540. }
  1541. static ssize_t alarms_store(struct device *device,
  1542. struct device_attribute *attr, const char *buf, size_t size)
  1543. {
  1544. struct w1_slave *sl = dev_to_w1_slave(device);
  1545. struct therm_info info;
  1546. u8 new_config_register[3]; /* array of data to be written */
  1547. int temp, ret;
  1548. char *token = NULL;
  1549. s8 tl, th; /* 1 byte per value + temp ring order */
  1550. char *p_args, *orig;
  1551. p_args = orig = kmalloc(size, GFP_KERNEL);
  1552. /* Safe string copys as buf is const */
  1553. if (!p_args) {
  1554. dev_warn(device,
  1555. "%s: error unable to allocate memory %d\n",
  1556. __func__, -ENOMEM);
  1557. return size;
  1558. }
  1559. strcpy(p_args, buf);
  1560. /* Split string using space char */
  1561. token = strsep(&p_args, " ");
  1562. if (!token) {
  1563. dev_info(device,
  1564. "%s: error parsing args %d\n", __func__, -EINVAL);
  1565. goto free_m;
  1566. }
  1567. /* Convert 1st entry to int */
  1568. ret = kstrtoint (token, 10, &temp);
  1569. if (ret) {
  1570. dev_info(device,
  1571. "%s: error parsing args %d\n", __func__, ret);
  1572. goto free_m;
  1573. }
  1574. tl = int_to_short(temp);
  1575. /* Split string using space char */
  1576. token = strsep(&p_args, " ");
  1577. if (!token) {
  1578. dev_info(device,
  1579. "%s: error parsing args %d\n", __func__, -EINVAL);
  1580. goto free_m;
  1581. }
  1582. /* Convert 2nd entry to int */
  1583. ret = kstrtoint (token, 10, &temp);
  1584. if (ret) {
  1585. dev_info(device,
  1586. "%s: error parsing args %d\n", __func__, ret);
  1587. goto free_m;
  1588. }
  1589. /* Prepare to cast to short by eliminating out of range values */
  1590. th = int_to_short(temp);
  1591. /* Reorder if required th and tl */
  1592. if (tl > th)
  1593. swap(tl, th);
  1594. /*
  1595. * Read the scratchpad to change only the required bits
  1596. * (th : byte 2 - tl: byte 3)
  1597. */
  1598. ret = read_scratchpad(sl, &info);
  1599. if (!ret) {
  1600. new_config_register[0] = th; /* Byte 2 */
  1601. new_config_register[1] = tl; /* Byte 3 */
  1602. new_config_register[2] = info.rom[4];/* Byte 4 */
  1603. } else {
  1604. dev_info(device,
  1605. "%s: error reading from the slave device %d\n",
  1606. __func__, ret);
  1607. goto free_m;
  1608. }
  1609. /* Write data in the device RAM */
  1610. if (!SLAVE_SPECIFIC_FUNC(sl)) {
  1611. dev_info(device,
  1612. "%s: Device not supported by the driver %d\n",
  1613. __func__, -ENODEV);
  1614. goto free_m;
  1615. }
  1616. ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
  1617. if (ret)
  1618. dev_info(device,
  1619. "%s: error writing to the slave device %d\n",
  1620. __func__, ret);
  1621. free_m:
  1622. /* free allocated memory */
  1623. kfree(orig);
  1624. return size;
  1625. }
  1626. static ssize_t therm_bulk_read_store(struct device *device,
  1627. struct device_attribute *attr, const char *buf, size_t size)
  1628. {
  1629. struct w1_master *dev_master = dev_to_w1_master(device);
  1630. int ret = -EINVAL; /* Invalid argument */
  1631. if (size == sizeof(BULK_TRIGGER_CMD))
  1632. if (!strncmp(buf, BULK_TRIGGER_CMD,
  1633. sizeof(BULK_TRIGGER_CMD)-1))
  1634. ret = trigger_bulk_read(dev_master);
  1635. if (ret)
  1636. dev_info(device,
  1637. "%s: unable to trigger a bulk read on the bus. err=%d\n",
  1638. __func__, ret);
  1639. return size;
  1640. }
  1641. static ssize_t therm_bulk_read_show(struct device *device,
  1642. struct device_attribute *attr, char *buf)
  1643. {
  1644. struct w1_master *dev_master = dev_to_w1_master(device);
  1645. struct w1_slave *sl = NULL;
  1646. int ret = 0;
  1647. list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
  1648. if (sl->family_data) {
  1649. if (bulk_read_support(sl)) {
  1650. if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
  1651. ret = -1;
  1652. goto show_result;
  1653. }
  1654. if (SLAVE_CONVERT_TRIGGERED(sl) == 1)
  1655. /* continue to check other slaves */
  1656. ret = 1;
  1657. }
  1658. }
  1659. }
  1660. show_result:
  1661. return sprintf(buf, "%d\n", ret);
  1662. }
  1663. static ssize_t conv_time_show(struct device *device,
  1664. struct device_attribute *attr, char *buf)
  1665. {
  1666. struct w1_slave *sl = dev_to_w1_slave(device);
  1667. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1668. dev_info(device,
  1669. "%s: Device is not supported by the driver\n", __func__);
  1670. return 0; /* No device family */
  1671. }
  1672. return sprintf(buf, "%d\n", conversion_time(sl));
  1673. }
  1674. static ssize_t conv_time_store(struct device *device,
  1675. struct device_attribute *attr, const char *buf, size_t size)
  1676. {
  1677. int val, ret = 0;
  1678. struct w1_slave *sl = dev_to_w1_slave(device);
  1679. if (kstrtoint(buf, 10, &val)) /* converting user entry to int */
  1680. return -EINVAL;
  1681. if (check_family_data(sl))
  1682. return -ENODEV;
  1683. if (val != CONV_TIME_MEASURE) {
  1684. if (val >= CONV_TIME_DEFAULT)
  1685. SLAVE_CONV_TIME_OVERRIDE(sl) = val;
  1686. else
  1687. return -EINVAL;
  1688. } else {
  1689. int conv_time;
  1690. ret = conv_time_measure(sl, &conv_time);
  1691. if (ret)
  1692. return -EIO;
  1693. SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time;
  1694. }
  1695. return size;
  1696. }
  1697. static ssize_t features_show(struct device *device,
  1698. struct device_attribute *attr, char *buf)
  1699. {
  1700. struct w1_slave *sl = dev_to_w1_slave(device);
  1701. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1702. dev_info(device,
  1703. "%s: Device not supported by the driver\n", __func__);
  1704. return 0; /* No device family */
  1705. }
  1706. return sprintf(buf, "%u\n", SLAVE_FEATURES(sl));
  1707. }
  1708. static ssize_t features_store(struct device *device,
  1709. struct device_attribute *attr, const char *buf, size_t size)
  1710. {
  1711. int val, ret = 0;
  1712. bool strong_pullup;
  1713. struct w1_slave *sl = dev_to_w1_slave(device);
  1714. ret = kstrtouint(buf, 10, &val); /* converting user entry to int */
  1715. if (ret)
  1716. return -EINVAL; /* invalid number */
  1717. if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
  1718. dev_info(device, "%s: Device not supported by the driver\n", __func__);
  1719. return -ENODEV;
  1720. }
  1721. if ((val & W1_THERM_FEATURES_MASK) != val)
  1722. return -EINVAL;
  1723. SLAVE_FEATURES(sl) = val;
  1724. strong_pullup = (w1_strong_pullup == 2 ||
  1725. (!SLAVE_POWERMODE(sl) &&
  1726. w1_strong_pullup));
  1727. if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
  1728. dev_warn(&sl->dev,
  1729. "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n",
  1730. __func__);
  1731. SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
  1732. }
  1733. return size;
  1734. }
  1735. #if IS_REACHABLE(CONFIG_HWMON)
  1736. static int w1_read_temp(struct device *device, u32 attr, int channel,
  1737. long *val)
  1738. {
  1739. struct w1_slave *sl = dev_get_drvdata(device);
  1740. struct therm_info info;
  1741. int ret;
  1742. switch (attr) {
  1743. case hwmon_temp_input:
  1744. ret = convert_t(sl, &info);
  1745. if (ret)
  1746. return ret;
  1747. if (!info.verdict) {
  1748. ret = -EIO;
  1749. return ret;
  1750. }
  1751. *val = temperature_from_RAM(sl, info.rom);
  1752. ret = 0;
  1753. break;
  1754. default:
  1755. ret = -EOPNOTSUPP;
  1756. break;
  1757. }
  1758. return ret;
  1759. }
  1760. #endif
  1761. #define W1_42_CHAIN 0x99
  1762. #define W1_42_CHAIN_OFF 0x3C
  1763. #define W1_42_CHAIN_OFF_INV 0xC3
  1764. #define W1_42_CHAIN_ON 0x5A
  1765. #define W1_42_CHAIN_ON_INV 0xA5
  1766. #define W1_42_CHAIN_DONE 0x96
  1767. #define W1_42_CHAIN_DONE_INV 0x69
  1768. #define W1_42_COND_READ 0x0F
  1769. #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
  1770. #define W1_42_FINISHED_BYTE 0xFF
  1771. static ssize_t w1_seq_show(struct device *device,
  1772. struct device_attribute *attr, char *buf)
  1773. {
  1774. struct w1_slave *sl = dev_to_w1_slave(device);
  1775. ssize_t c = PAGE_SIZE;
  1776. int i;
  1777. u8 ack;
  1778. u64 rn;
  1779. struct w1_reg_num *reg_num;
  1780. int seq = 0;
  1781. mutex_lock(&sl->master->bus_mutex);
  1782. /* Place all devices in CHAIN state */
  1783. if (w1_reset_bus(sl->master))
  1784. goto error;
  1785. w1_write_8(sl->master, W1_SKIP_ROM);
  1786. w1_write_8(sl->master, W1_42_CHAIN);
  1787. w1_write_8(sl->master, W1_42_CHAIN_ON);
  1788. w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
  1789. msleep(sl->master->pullup_duration);
  1790. /* check for acknowledgment */
  1791. ack = w1_read_8(sl->master);
  1792. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  1793. goto error;
  1794. /* In case the bus fails to send 0xFF, limit */
  1795. for (i = 0; i <= 64; i++) {
  1796. if (w1_reset_bus(sl->master))
  1797. goto error;
  1798. w1_write_8(sl->master, W1_42_COND_READ);
  1799. w1_read_block(sl->master, (u8 *)&rn, 8);
  1800. reg_num = (struct w1_reg_num *) &rn;
  1801. if (reg_num->family == W1_42_FINISHED_BYTE)
  1802. break;
  1803. if (sl->reg_num.id == reg_num->id)
  1804. seq = i;
  1805. if (w1_reset_bus(sl->master))
  1806. goto error;
  1807. /* Put the device into chain DONE state */
  1808. w1_write_8(sl->master, W1_MATCH_ROM);
  1809. w1_write_block(sl->master, (u8 *)&rn, 8);
  1810. w1_write_8(sl->master, W1_42_CHAIN);
  1811. w1_write_8(sl->master, W1_42_CHAIN_DONE);
  1812. w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
  1813. /* check for acknowledgment */
  1814. ack = w1_read_8(sl->master);
  1815. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  1816. goto error;
  1817. }
  1818. /* Exit from CHAIN state */
  1819. if (w1_reset_bus(sl->master))
  1820. goto error;
  1821. w1_write_8(sl->master, W1_SKIP_ROM);
  1822. w1_write_8(sl->master, W1_42_CHAIN);
  1823. w1_write_8(sl->master, W1_42_CHAIN_OFF);
  1824. w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
  1825. /* check for acknowledgment */
  1826. ack = w1_read_8(sl->master);
  1827. if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
  1828. goto error;
  1829. mutex_unlock(&sl->master->bus_mutex);
  1830. c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
  1831. return PAGE_SIZE - c;
  1832. error:
  1833. mutex_unlock(&sl->master->bus_mutex);
  1834. return -EIO;
  1835. }
  1836. static int __init w1_therm_init(void)
  1837. {
  1838. int err, i;
  1839. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
  1840. err = w1_register_family(w1_therm_families[i].f);
  1841. if (err)
  1842. w1_therm_families[i].broken = 1;
  1843. }
  1844. return 0;
  1845. }
  1846. static void __exit w1_therm_fini(void)
  1847. {
  1848. int i;
  1849. for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
  1850. if (!w1_therm_families[i].broken)
  1851. w1_unregister_family(w1_therm_families[i].f);
  1852. }
  1853. module_init(w1_therm_init);
  1854. module_exit(w1_therm_fini);
  1855. MODULE_AUTHOR("Evgeniy Polyakov <[email protected]>");
  1856. MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
  1857. MODULE_LICENSE("GPL");
  1858. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
  1859. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
  1860. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
  1861. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
  1862. MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));