asc7621.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /*
  2. * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3. * Copyright (c) 2007, 2010 George Joseph <george.joseph@fairview5.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. /* Addresses to scan */
  29. static const unsigned short normal_i2c[] = {
  30. 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
  31. };
  32. enum asc7621_type {
  33. asc7621,
  34. asc7621a
  35. };
  36. #define INTERVAL_HIGH (HZ + HZ / 2)
  37. #define INTERVAL_LOW (1 * 60 * HZ)
  38. #define PRI_NONE 0
  39. #define PRI_LOW 1
  40. #define PRI_HIGH 2
  41. #define FIRST_CHIP asc7621
  42. #define LAST_CHIP asc7621a
  43. struct asc7621_chip {
  44. char *name;
  45. enum asc7621_type chip_type;
  46. u8 company_reg;
  47. u8 company_id;
  48. u8 verstep_reg;
  49. u8 verstep_id;
  50. const unsigned short *addresses;
  51. };
  52. static struct asc7621_chip asc7621_chips[] = {
  53. {
  54. .name = "asc7621",
  55. .chip_type = asc7621,
  56. .company_reg = 0x3e,
  57. .company_id = 0x61,
  58. .verstep_reg = 0x3f,
  59. .verstep_id = 0x6c,
  60. .addresses = normal_i2c,
  61. },
  62. {
  63. .name = "asc7621a",
  64. .chip_type = asc7621a,
  65. .company_reg = 0x3e,
  66. .company_id = 0x61,
  67. .verstep_reg = 0x3f,
  68. .verstep_id = 0x6d,
  69. .addresses = normal_i2c,
  70. },
  71. };
  72. /*
  73. * Defines the highest register to be used, not the count.
  74. * The actual count will probably be smaller because of gaps
  75. * in the implementation (unused register locations).
  76. * This define will safely set the array size of both the parameter
  77. * and data arrays.
  78. * This comes from the data sheet register description table.
  79. */
  80. #define LAST_REGISTER 0xff
  81. struct asc7621_data {
  82. struct i2c_client client;
  83. struct device *class_dev;
  84. struct mutex update_lock;
  85. int valid; /* !=0 if following fields are valid */
  86. unsigned long last_high_reading; /* In jiffies */
  87. unsigned long last_low_reading; /* In jiffies */
  88. /*
  89. * Registers we care about occupy the corresponding index
  90. * in the array. Registers we don't care about are left
  91. * at 0.
  92. */
  93. u8 reg[LAST_REGISTER + 1];
  94. };
  95. /*
  96. * Macro to get the parent asc7621_param structure
  97. * from a sensor_device_attribute passed into the
  98. * show/store functions.
  99. */
  100. #define to_asc7621_param(_sda) \
  101. container_of(_sda, struct asc7621_param, sda)
  102. /*
  103. * Each parameter to be retrieved needs an asc7621_param structure
  104. * allocated. It contains the sensor_device_attribute structure
  105. * and the control info needed to retrieve the value from the register map.
  106. */
  107. struct asc7621_param {
  108. struct sensor_device_attribute sda;
  109. u8 priority;
  110. u8 msb[3];
  111. u8 lsb[3];
  112. u8 mask[3];
  113. u8 shift[3];
  114. };
  115. /*
  116. * This is the map that ultimately indicates whether we'll be
  117. * retrieving a register value or not, and at what frequency.
  118. */
  119. static u8 asc7621_register_priorities[255];
  120. static struct asc7621_data *asc7621_update_device(struct device *dev);
  121. static inline u8 read_byte(struct i2c_client *client, u8 reg)
  122. {
  123. int res = i2c_smbus_read_byte_data(client, reg);
  124. if (res < 0) {
  125. dev_err(&client->dev,
  126. "Unable to read from register 0x%02x.\n", reg);
  127. return 0;
  128. }
  129. return res & 0xff;
  130. }
  131. static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
  132. {
  133. int res = i2c_smbus_write_byte_data(client, reg, data);
  134. if (res < 0) {
  135. dev_err(&client->dev,
  136. "Unable to write value 0x%02x to register 0x%02x.\n",
  137. data, reg);
  138. }
  139. return res;
  140. }
  141. /*
  142. * Data Handlers
  143. * Each function handles the formatting, storage
  144. * and retrieval of like parameters.
  145. */
  146. #define SETUP_SHOW_DATA_PARAM(d, a) \
  147. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  148. struct asc7621_data *data = asc7621_update_device(d); \
  149. struct asc7621_param *param = to_asc7621_param(sda)
  150. #define SETUP_STORE_DATA_PARAM(d, a) \
  151. struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
  152. struct i2c_client *client = to_i2c_client(d); \
  153. struct asc7621_data *data = i2c_get_clientdata(client); \
  154. struct asc7621_param *param = to_asc7621_param(sda)
  155. /*
  156. * u8 is just what it sounds like...an unsigned byte with no
  157. * special formatting.
  158. */
  159. static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
  160. char *buf)
  161. {
  162. SETUP_SHOW_DATA_PARAM(dev, attr);
  163. return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
  164. }
  165. static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. SETUP_STORE_DATA_PARAM(dev, attr);
  169. long reqval;
  170. if (kstrtol(buf, 10, &reqval))
  171. return -EINVAL;
  172. reqval = clamp_val(reqval, 0, 255);
  173. mutex_lock(&data->update_lock);
  174. data->reg[param->msb[0]] = reqval;
  175. write_byte(client, param->msb[0], reqval);
  176. mutex_unlock(&data->update_lock);
  177. return count;
  178. }
  179. /*
  180. * Many of the config values occupy only a few bits of a register.
  181. */
  182. static ssize_t show_bitmask(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. SETUP_SHOW_DATA_PARAM(dev, attr);
  186. return sprintf(buf, "%u\n",
  187. (data->reg[param->msb[0]] >> param->
  188. shift[0]) & param->mask[0]);
  189. }
  190. static ssize_t store_bitmask(struct device *dev,
  191. struct device_attribute *attr,
  192. const char *buf, size_t count)
  193. {
  194. SETUP_STORE_DATA_PARAM(dev, attr);
  195. long reqval;
  196. u8 currval;
  197. if (kstrtol(buf, 10, &reqval))
  198. return -EINVAL;
  199. reqval = clamp_val(reqval, 0, param->mask[0]);
  200. reqval = (reqval & param->mask[0]) << param->shift[0];
  201. mutex_lock(&data->update_lock);
  202. currval = read_byte(client, param->msb[0]);
  203. reqval |= (currval & ~(param->mask[0] << param->shift[0]));
  204. data->reg[param->msb[0]] = reqval;
  205. write_byte(client, param->msb[0], reqval);
  206. mutex_unlock(&data->update_lock);
  207. return count;
  208. }
  209. /*
  210. * 16 bit fan rpm values
  211. * reported by the device as the number of 11.111us periods (90khz)
  212. * between full fan rotations. Therefore...
  213. * RPM = (90000 * 60) / register value
  214. */
  215. static ssize_t show_fan16(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. SETUP_SHOW_DATA_PARAM(dev, attr);
  219. u16 regval;
  220. mutex_lock(&data->update_lock);
  221. regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
  222. mutex_unlock(&data->update_lock);
  223. return sprintf(buf, "%u\n",
  224. (regval == 0 ? -1 : (regval) ==
  225. 0xffff ? 0 : 5400000 / regval));
  226. }
  227. static ssize_t store_fan16(struct device *dev,
  228. struct device_attribute *attr, const char *buf,
  229. size_t count)
  230. {
  231. SETUP_STORE_DATA_PARAM(dev, attr);
  232. long reqval;
  233. if (kstrtol(buf, 10, &reqval))
  234. return -EINVAL;
  235. /*
  236. * If a minimum RPM of zero is requested, then we set the register to
  237. * 0xffff. This value allows the fan to be stopped completely without
  238. * generating an alarm.
  239. */
  240. reqval =
  241. (reqval <= 0 ? 0xffff : clamp_val(5400000 / reqval, 0, 0xfffe));
  242. mutex_lock(&data->update_lock);
  243. data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
  244. data->reg[param->lsb[0]] = reqval & 0xff;
  245. write_byte(client, param->msb[0], data->reg[param->msb[0]]);
  246. write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
  247. mutex_unlock(&data->update_lock);
  248. return count;
  249. }
  250. /*
  251. * Voltages are scaled in the device so that the nominal voltage
  252. * is 3/4ths of the 0-255 range (i.e. 192).
  253. * If all voltages are 'normal' then all voltage registers will
  254. * read 0xC0.
  255. *
  256. * The data sheet provides us with the 3/4 scale value for each voltage
  257. * which is stored in in_scaling. The sda->index parameter value provides
  258. * the index into in_scaling.
  259. *
  260. * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
  261. * respectively. That doesn't mean that's what the motherboard provides. :)
  262. */
  263. static const int asc7621_in_scaling[] = {
  264. 2500, 2250, 3300, 5000, 12000
  265. };
  266. static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
  267. char *buf)
  268. {
  269. SETUP_SHOW_DATA_PARAM(dev, attr);
  270. u16 regval;
  271. u8 nr = sda->index;
  272. mutex_lock(&data->update_lock);
  273. regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
  274. mutex_unlock(&data->update_lock);
  275. /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
  276. regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
  277. return sprintf(buf, "%u\n", regval);
  278. }
  279. /* 8 bit voltage values (the mins and maxs) */
  280. static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
  281. char *buf)
  282. {
  283. SETUP_SHOW_DATA_PARAM(dev, attr);
  284. u8 nr = sda->index;
  285. return sprintf(buf, "%u\n",
  286. ((data->reg[param->msb[0]] *
  287. asc7621_in_scaling[nr]) / 0xc0));
  288. }
  289. static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
  290. const char *buf, size_t count)
  291. {
  292. SETUP_STORE_DATA_PARAM(dev, attr);
  293. long reqval;
  294. u8 nr = sda->index;
  295. if (kstrtol(buf, 10, &reqval))
  296. return -EINVAL;
  297. reqval = clamp_val(reqval, 0, 0xffff);
  298. reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
  299. reqval = clamp_val(reqval, 0, 0xff);
  300. mutex_lock(&data->update_lock);
  301. data->reg[param->msb[0]] = reqval;
  302. write_byte(client, param->msb[0], reqval);
  303. mutex_unlock(&data->update_lock);
  304. return count;
  305. }
  306. static ssize_t show_temp8(struct device *dev,
  307. struct device_attribute *attr, char *buf)
  308. {
  309. SETUP_SHOW_DATA_PARAM(dev, attr);
  310. return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
  311. }
  312. static ssize_t store_temp8(struct device *dev,
  313. struct device_attribute *attr, const char *buf,
  314. size_t count)
  315. {
  316. SETUP_STORE_DATA_PARAM(dev, attr);
  317. long reqval;
  318. s8 temp;
  319. if (kstrtol(buf, 10, &reqval))
  320. return -EINVAL;
  321. reqval = clamp_val(reqval, -127000, 127000);
  322. temp = reqval / 1000;
  323. mutex_lock(&data->update_lock);
  324. data->reg[param->msb[0]] = temp;
  325. write_byte(client, param->msb[0], temp);
  326. mutex_unlock(&data->update_lock);
  327. return count;
  328. }
  329. /*
  330. * Temperatures that occupy 2 bytes always have the whole
  331. * number of degrees in the MSB with some part of the LSB
  332. * indicating fractional degrees.
  333. */
  334. /* mmmmmmmm.llxxxxxx */
  335. static ssize_t show_temp10(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. SETUP_SHOW_DATA_PARAM(dev, attr);
  339. u8 msb, lsb;
  340. int temp;
  341. mutex_lock(&data->update_lock);
  342. msb = data->reg[param->msb[0]];
  343. lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
  344. temp = (((s8) msb) * 1000) + (lsb * 250);
  345. mutex_unlock(&data->update_lock);
  346. return sprintf(buf, "%d\n", temp);
  347. }
  348. /* mmmmmm.ll */
  349. static ssize_t show_temp62(struct device *dev,
  350. struct device_attribute *attr, char *buf)
  351. {
  352. SETUP_SHOW_DATA_PARAM(dev, attr);
  353. u8 regval = data->reg[param->msb[0]];
  354. int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
  355. return sprintf(buf, "%d\n", temp);
  356. }
  357. static ssize_t store_temp62(struct device *dev,
  358. struct device_attribute *attr, const char *buf,
  359. size_t count)
  360. {
  361. SETUP_STORE_DATA_PARAM(dev, attr);
  362. long reqval, i, f;
  363. s8 temp;
  364. if (kstrtol(buf, 10, &reqval))
  365. return -EINVAL;
  366. reqval = clamp_val(reqval, -32000, 31750);
  367. i = reqval / 1000;
  368. f = reqval - (i * 1000);
  369. temp = i << 2;
  370. temp |= f / 250;
  371. mutex_lock(&data->update_lock);
  372. data->reg[param->msb[0]] = temp;
  373. write_byte(client, param->msb[0], temp);
  374. mutex_unlock(&data->update_lock);
  375. return count;
  376. }
  377. /*
  378. * The aSC7621 doesn't provide an "auto_point2". Instead, you
  379. * specify the auto_point1 and a range. To keep with the sysfs
  380. * hwmon specs, we synthesize the auto_point_2 from them.
  381. */
  382. static const u32 asc7621_range_map[] = {
  383. 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
  384. 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
  385. };
  386. static ssize_t show_ap2_temp(struct device *dev,
  387. struct device_attribute *attr, char *buf)
  388. {
  389. SETUP_SHOW_DATA_PARAM(dev, attr);
  390. long auto_point1;
  391. u8 regval;
  392. int temp;
  393. mutex_lock(&data->update_lock);
  394. auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
  395. regval =
  396. ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
  397. temp = auto_point1 + asc7621_range_map[clamp_val(regval, 0, 15)];
  398. mutex_unlock(&data->update_lock);
  399. return sprintf(buf, "%d\n", temp);
  400. }
  401. static ssize_t store_ap2_temp(struct device *dev,
  402. struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. SETUP_STORE_DATA_PARAM(dev, attr);
  406. long reqval, auto_point1;
  407. int i;
  408. u8 currval, newval = 0;
  409. if (kstrtol(buf, 10, &reqval))
  410. return -EINVAL;
  411. mutex_lock(&data->update_lock);
  412. auto_point1 = data->reg[param->msb[1]] * 1000;
  413. reqval = clamp_val(reqval, auto_point1 + 2000, auto_point1 + 80000);
  414. for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
  415. if (reqval >= auto_point1 + asc7621_range_map[i]) {
  416. newval = i;
  417. break;
  418. }
  419. }
  420. newval = (newval & param->mask[0]) << param->shift[0];
  421. currval = read_byte(client, param->msb[0]);
  422. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  423. data->reg[param->msb[0]] = newval;
  424. write_byte(client, param->msb[0], newval);
  425. mutex_unlock(&data->update_lock);
  426. return count;
  427. }
  428. static ssize_t show_pwm_ac(struct device *dev,
  429. struct device_attribute *attr, char *buf)
  430. {
  431. SETUP_SHOW_DATA_PARAM(dev, attr);
  432. u8 config, altbit, regval;
  433. const u8 map[] = {
  434. 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
  435. 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
  436. };
  437. mutex_lock(&data->update_lock);
  438. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  439. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  440. regval = config | (altbit << 3);
  441. mutex_unlock(&data->update_lock);
  442. return sprintf(buf, "%u\n", map[clamp_val(regval, 0, 15)]);
  443. }
  444. static ssize_t store_pwm_ac(struct device *dev,
  445. struct device_attribute *attr,
  446. const char *buf, size_t count)
  447. {
  448. SETUP_STORE_DATA_PARAM(dev, attr);
  449. unsigned long reqval;
  450. u8 currval, config, altbit, newval;
  451. const u16 map[] = {
  452. 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
  453. 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
  454. 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  455. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
  456. };
  457. if (kstrtoul(buf, 10, &reqval))
  458. return -EINVAL;
  459. if (reqval > 31)
  460. return -EINVAL;
  461. reqval = map[reqval];
  462. if (reqval == 0xff)
  463. return -EINVAL;
  464. config = reqval & 0x07;
  465. altbit = (reqval >> 3) & 0x01;
  466. config = (config & param->mask[0]) << param->shift[0];
  467. altbit = (altbit & param->mask[1]) << param->shift[1];
  468. mutex_lock(&data->update_lock);
  469. currval = read_byte(client, param->msb[0]);
  470. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  471. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  472. data->reg[param->msb[0]] = newval;
  473. write_byte(client, param->msb[0], newval);
  474. mutex_unlock(&data->update_lock);
  475. return count;
  476. }
  477. static ssize_t show_pwm_enable(struct device *dev,
  478. struct device_attribute *attr, char *buf)
  479. {
  480. SETUP_SHOW_DATA_PARAM(dev, attr);
  481. u8 config, altbit, minoff, val, newval;
  482. mutex_lock(&data->update_lock);
  483. config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  484. altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
  485. minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
  486. mutex_unlock(&data->update_lock);
  487. val = config | (altbit << 3);
  488. newval = 0;
  489. if (val == 3 || val >= 10)
  490. newval = 255;
  491. else if (val == 4)
  492. newval = 0;
  493. else if (val == 7)
  494. newval = 1;
  495. else if (minoff == 1)
  496. newval = 2;
  497. else
  498. newval = 3;
  499. return sprintf(buf, "%u\n", newval);
  500. }
  501. static ssize_t store_pwm_enable(struct device *dev,
  502. struct device_attribute *attr,
  503. const char *buf, size_t count)
  504. {
  505. SETUP_STORE_DATA_PARAM(dev, attr);
  506. long reqval;
  507. u8 currval, config, altbit, newval, minoff = 255;
  508. if (kstrtol(buf, 10, &reqval))
  509. return -EINVAL;
  510. switch (reqval) {
  511. case 0:
  512. newval = 0x04;
  513. break;
  514. case 1:
  515. newval = 0x07;
  516. break;
  517. case 2:
  518. newval = 0x00;
  519. minoff = 1;
  520. break;
  521. case 3:
  522. newval = 0x00;
  523. minoff = 0;
  524. break;
  525. case 255:
  526. newval = 0x03;
  527. break;
  528. default:
  529. return -EINVAL;
  530. }
  531. config = newval & 0x07;
  532. altbit = (newval >> 3) & 0x01;
  533. mutex_lock(&data->update_lock);
  534. config = (config & param->mask[0]) << param->shift[0];
  535. altbit = (altbit & param->mask[1]) << param->shift[1];
  536. currval = read_byte(client, param->msb[0]);
  537. newval = config | (currval & ~(param->mask[0] << param->shift[0]));
  538. newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
  539. data->reg[param->msb[0]] = newval;
  540. write_byte(client, param->msb[0], newval);
  541. if (minoff < 255) {
  542. minoff = (minoff & param->mask[2]) << param->shift[2];
  543. currval = read_byte(client, param->msb[2]);
  544. newval =
  545. minoff | (currval & ~(param->mask[2] << param->shift[2]));
  546. data->reg[param->msb[2]] = newval;
  547. write_byte(client, param->msb[2], newval);
  548. }
  549. mutex_unlock(&data->update_lock);
  550. return count;
  551. }
  552. static const u32 asc7621_pwm_freq_map[] = {
  553. 10, 15, 23, 30, 38, 47, 62, 94,
  554. 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
  555. };
  556. static ssize_t show_pwm_freq(struct device *dev,
  557. struct device_attribute *attr, char *buf)
  558. {
  559. SETUP_SHOW_DATA_PARAM(dev, attr);
  560. u8 regval =
  561. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  562. regval = clamp_val(regval, 0, 15);
  563. return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
  564. }
  565. static ssize_t store_pwm_freq(struct device *dev,
  566. struct device_attribute *attr,
  567. const char *buf, size_t count)
  568. {
  569. SETUP_STORE_DATA_PARAM(dev, attr);
  570. unsigned long reqval;
  571. u8 currval, newval = 255;
  572. int i;
  573. if (kstrtoul(buf, 10, &reqval))
  574. return -EINVAL;
  575. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
  576. if (reqval == asc7621_pwm_freq_map[i]) {
  577. newval = i;
  578. break;
  579. }
  580. }
  581. if (newval == 255)
  582. return -EINVAL;
  583. newval = (newval & param->mask[0]) << param->shift[0];
  584. mutex_lock(&data->update_lock);
  585. currval = read_byte(client, param->msb[0]);
  586. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  587. data->reg[param->msb[0]] = newval;
  588. write_byte(client, param->msb[0], newval);
  589. mutex_unlock(&data->update_lock);
  590. return count;
  591. }
  592. static const u32 asc7621_pwm_auto_spinup_map[] = {
  593. 0, 100, 250, 400, 700, 1000, 2000, 4000
  594. };
  595. static ssize_t show_pwm_ast(struct device *dev,
  596. struct device_attribute *attr, char *buf)
  597. {
  598. SETUP_SHOW_DATA_PARAM(dev, attr);
  599. u8 regval =
  600. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  601. regval = clamp_val(regval, 0, 7);
  602. return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
  603. }
  604. static ssize_t store_pwm_ast(struct device *dev,
  605. struct device_attribute *attr,
  606. const char *buf, size_t count)
  607. {
  608. SETUP_STORE_DATA_PARAM(dev, attr);
  609. long reqval;
  610. u8 currval, newval = 255;
  611. u32 i;
  612. if (kstrtol(buf, 10, &reqval))
  613. return -EINVAL;
  614. for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
  615. if (reqval == asc7621_pwm_auto_spinup_map[i]) {
  616. newval = i;
  617. break;
  618. }
  619. }
  620. if (newval == 255)
  621. return -EINVAL;
  622. newval = (newval & param->mask[0]) << param->shift[0];
  623. mutex_lock(&data->update_lock);
  624. currval = read_byte(client, param->msb[0]);
  625. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  626. data->reg[param->msb[0]] = newval;
  627. write_byte(client, param->msb[0], newval);
  628. mutex_unlock(&data->update_lock);
  629. return count;
  630. }
  631. static const u32 asc7621_temp_smoothing_time_map[] = {
  632. 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
  633. };
  634. static ssize_t show_temp_st(struct device *dev,
  635. struct device_attribute *attr, char *buf)
  636. {
  637. SETUP_SHOW_DATA_PARAM(dev, attr);
  638. u8 regval =
  639. (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
  640. regval = clamp_val(regval, 0, 7);
  641. return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
  642. }
  643. static ssize_t store_temp_st(struct device *dev,
  644. struct device_attribute *attr,
  645. const char *buf, size_t count)
  646. {
  647. SETUP_STORE_DATA_PARAM(dev, attr);
  648. long reqval;
  649. u8 currval, newval = 255;
  650. u32 i;
  651. if (kstrtol(buf, 10, &reqval))
  652. return -EINVAL;
  653. for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
  654. if (reqval == asc7621_temp_smoothing_time_map[i]) {
  655. newval = i;
  656. break;
  657. }
  658. }
  659. if (newval == 255)
  660. return -EINVAL;
  661. newval = (newval & param->mask[0]) << param->shift[0];
  662. mutex_lock(&data->update_lock);
  663. currval = read_byte(client, param->msb[0]);
  664. newval |= (currval & ~(param->mask[0] << param->shift[0]));
  665. data->reg[param->msb[0]] = newval;
  666. write_byte(client, param->msb[0], newval);
  667. mutex_unlock(&data->update_lock);
  668. return count;
  669. }
  670. /*
  671. * End of data handlers
  672. *
  673. * These defines do nothing more than make the table easier
  674. * to read when wrapped at column 80.
  675. */
  676. /*
  677. * Creates a variable length array inititalizer.
  678. * VAA(1,3,5,7) would produce {1,3,5,7}
  679. */
  680. #define VAA(args...) {args}
  681. #define PREAD(name, n, pri, rm, rl, m, s, r) \
  682. {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
  683. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  684. .shift[0] = s,}
  685. #define PWRITE(name, n, pri, rm, rl, m, s, r) \
  686. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  687. .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
  688. .shift[0] = s,}
  689. /*
  690. * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
  691. * were created using the VAA macro.
  692. */
  693. #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
  694. {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
  695. .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
  696. static struct asc7621_param asc7621_params[] = {
  697. PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
  698. PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
  699. PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
  700. PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
  701. PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
  702. PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
  703. PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
  704. PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
  705. PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
  706. PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
  707. PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
  708. PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
  709. PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
  710. PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
  711. PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
  712. PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
  713. PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
  714. PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
  715. PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
  716. PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
  717. PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
  718. PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
  719. PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
  720. PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
  721. PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
  722. PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
  723. PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
  724. PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
  725. PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
  726. PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
  727. PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
  728. PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
  729. PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
  730. PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
  731. PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
  732. PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
  733. PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
  734. PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
  735. PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
  736. PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
  737. PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
  738. PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
  739. PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
  740. PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
  741. PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
  742. PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
  743. PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
  744. PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
  745. PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
  746. PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
  747. PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
  748. PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
  749. PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
  750. PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
  751. PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
  752. PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
  753. PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
  754. PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
  755. PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
  756. PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
  757. PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
  758. PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
  759. PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
  760. PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
  761. PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  762. bitmask),
  763. PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  764. bitmask),
  765. PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  766. bitmask),
  767. PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  768. bitmask),
  769. PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
  770. bitmask),
  771. PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
  772. bitmask),
  773. PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
  774. bitmask),
  775. PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
  776. bitmask),
  777. PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
  778. PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
  779. PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
  780. PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
  781. PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
  782. VAA(0x0f), VAA(4), ap2_temp),
  783. PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
  784. VAA(0x0f), VAA(4), ap2_temp),
  785. PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
  786. VAA(0x0f), VAA(4), ap2_temp),
  787. PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
  788. VAA(0x0f), VAA(4), ap2_temp),
  789. PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
  790. PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
  791. PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
  792. PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
  793. PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
  794. PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
  795. PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
  796. PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
  797. PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
  798. PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
  799. PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
  800. PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
  801. PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
  802. PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
  803. PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
  804. PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
  805. PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
  806. VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
  807. PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
  808. VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
  809. PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
  810. VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
  811. PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
  812. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  813. PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
  814. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  815. PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
  816. VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
  817. PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
  818. PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
  819. PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
  820. PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
  821. PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
  822. PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
  823. PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
  824. PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
  825. PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
  826. PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
  827. PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
  828. PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
  829. PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
  830. PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
  831. PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
  832. PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
  833. PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
  834. PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
  835. PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
  836. PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
  837. PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
  838. };
  839. static struct asc7621_data *asc7621_update_device(struct device *dev)
  840. {
  841. struct i2c_client *client = to_i2c_client(dev);
  842. struct asc7621_data *data = i2c_get_clientdata(client);
  843. int i;
  844. /*
  845. * The asc7621 chips guarantee consistent reads of multi-byte values
  846. * regardless of the order of the reads. No special logic is needed
  847. * so we can just read the registers in whatever order they appear
  848. * in the asc7621_params array.
  849. */
  850. mutex_lock(&data->update_lock);
  851. /* Read all the high priority registers */
  852. if (!data->valid ||
  853. time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
  854. for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
  855. if (asc7621_register_priorities[i] == PRI_HIGH) {
  856. data->reg[i] =
  857. i2c_smbus_read_byte_data(client, i) & 0xff;
  858. }
  859. }
  860. data->last_high_reading = jiffies;
  861. } /* last_reading */
  862. /* Read all the low priority registers. */
  863. if (!data->valid ||
  864. time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
  865. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  866. if (asc7621_register_priorities[i] == PRI_LOW) {
  867. data->reg[i] =
  868. i2c_smbus_read_byte_data(client, i) & 0xff;
  869. }
  870. }
  871. data->last_low_reading = jiffies;
  872. } /* last_reading */
  873. data->valid = 1;
  874. mutex_unlock(&data->update_lock);
  875. return data;
  876. }
  877. /*
  878. * Standard detection and initialization below
  879. *
  880. * Helper function that checks if an address is valid
  881. * for a particular chip.
  882. */
  883. static inline int valid_address_for_chip(int chip_type, int address)
  884. {
  885. int i;
  886. for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
  887. i++) {
  888. if (asc7621_chips[chip_type].addresses[i] == address)
  889. return 1;
  890. }
  891. return 0;
  892. }
  893. static void asc7621_init_client(struct i2c_client *client)
  894. {
  895. int value;
  896. /* Warn if part was not "READY" */
  897. value = read_byte(client, 0x40);
  898. if (value & 0x02) {
  899. dev_err(&client->dev,
  900. "Client (%d,0x%02x) config is locked.\n",
  901. i2c_adapter_id(client->adapter), client->addr);
  902. }
  903. if (!(value & 0x04)) {
  904. dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
  905. i2c_adapter_id(client->adapter), client->addr);
  906. }
  907. /*
  908. * Start monitoring
  909. *
  910. * Try to clear LOCK, Set START, save everything else
  911. */
  912. value = (value & ~0x02) | 0x01;
  913. write_byte(client, 0x40, value & 0xff);
  914. }
  915. static int
  916. asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
  917. {
  918. struct asc7621_data *data;
  919. int i, err;
  920. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  921. return -EIO;
  922. data = devm_kzalloc(&client->dev, sizeof(struct asc7621_data),
  923. GFP_KERNEL);
  924. if (data == NULL)
  925. return -ENOMEM;
  926. i2c_set_clientdata(client, data);
  927. mutex_init(&data->update_lock);
  928. /* Initialize the asc7621 chip */
  929. asc7621_init_client(client);
  930. /* Create the sysfs entries */
  931. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  932. err =
  933. device_create_file(&client->dev,
  934. &(asc7621_params[i].sda.dev_attr));
  935. if (err)
  936. goto exit_remove;
  937. }
  938. data->class_dev = hwmon_device_register(&client->dev);
  939. if (IS_ERR(data->class_dev)) {
  940. err = PTR_ERR(data->class_dev);
  941. goto exit_remove;
  942. }
  943. return 0;
  944. exit_remove:
  945. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  946. device_remove_file(&client->dev,
  947. &(asc7621_params[i].sda.dev_attr));
  948. }
  949. return err;
  950. }
  951. static int asc7621_detect(struct i2c_client *client,
  952. struct i2c_board_info *info)
  953. {
  954. struct i2c_adapter *adapter = client->adapter;
  955. int company, verstep, chip_index;
  956. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  957. return -ENODEV;
  958. for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
  959. if (!valid_address_for_chip(chip_index, client->addr))
  960. continue;
  961. company = read_byte(client,
  962. asc7621_chips[chip_index].company_reg);
  963. verstep = read_byte(client,
  964. asc7621_chips[chip_index].verstep_reg);
  965. if (company == asc7621_chips[chip_index].company_id &&
  966. verstep == asc7621_chips[chip_index].verstep_id) {
  967. strlcpy(info->type, asc7621_chips[chip_index].name,
  968. I2C_NAME_SIZE);
  969. dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
  970. asc7621_chips[chip_index].name, client->addr);
  971. return 0;
  972. }
  973. }
  974. return -ENODEV;
  975. }
  976. static int asc7621_remove(struct i2c_client *client)
  977. {
  978. struct asc7621_data *data = i2c_get_clientdata(client);
  979. int i;
  980. hwmon_device_unregister(data->class_dev);
  981. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  982. device_remove_file(&client->dev,
  983. &(asc7621_params[i].sda.dev_attr));
  984. }
  985. return 0;
  986. }
  987. static const struct i2c_device_id asc7621_id[] = {
  988. {"asc7621", asc7621},
  989. {"asc7621a", asc7621a},
  990. {},
  991. };
  992. MODULE_DEVICE_TABLE(i2c, asc7621_id);
  993. static struct i2c_driver asc7621_driver = {
  994. .class = I2C_CLASS_HWMON,
  995. .driver = {
  996. .name = "asc7621",
  997. },
  998. .probe = asc7621_probe,
  999. .remove = asc7621_remove,
  1000. .id_table = asc7621_id,
  1001. .detect = asc7621_detect,
  1002. .address_list = normal_i2c,
  1003. };
  1004. static int __init sm_asc7621_init(void)
  1005. {
  1006. int i, j;
  1007. /*
  1008. * Collect all the registers needed into a single array.
  1009. * This way, if a register isn't actually used for anything,
  1010. * we don't retrieve it.
  1011. */
  1012. for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
  1013. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
  1014. asc7621_register_priorities[asc7621_params[i].msb[j]] =
  1015. asc7621_params[i].priority;
  1016. for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
  1017. asc7621_register_priorities[asc7621_params[i].lsb[j]] =
  1018. asc7621_params[i].priority;
  1019. }
  1020. return i2c_add_driver(&asc7621_driver);
  1021. }
  1022. static void __exit sm_asc7621_exit(void)
  1023. {
  1024. i2c_del_driver(&asc7621_driver);
  1025. }
  1026. MODULE_LICENSE("GPL");
  1027. MODULE_AUTHOR("George Joseph");
  1028. MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
  1029. module_init(sm_asc7621_init);
  1030. module_exit(sm_asc7621_exit);